所以我的程序读取一个 .txt 文件,并使用 .useDelimeter 来分隔宠物并将它们放入数组列表中:
try{
Scanner petReader = new Scanner(new File("pet3-dogs.txt"));
petReader.useDelimiter(",");
String line2 = petReader.nextLine();
while(petReader.hasNextLine()){ //the while loop stores each attribute in the appropriate variable and arraylist of petshops while there's another line.
petReader.useDelimiter(",");
String shop =petReader.next();
String type =petReader.next();
double price = Double.parseDouble(petReader.next());
Date date = df.parse(petReader.next());
String notes =petReader.nextLine();
String size =petReader.nextLine();
String neutered =petReader.nextLine();
petReader.useDelimiter(",");
pets.add(new Pet(shop, type, price, date, notes, size, neutered));
System.out.println(pets.toString());
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, e); //if the program wasn't able to read the file, it will display a message dialog.
}
这是输出。事实上,它在最后两个变量中获取下一个宠物店:
[Solly's Pet Store
Giant Schnauzer
£176.43
Wed Jul 07 00:00:00 BST 2010
,none,Medium,no
The Menagerie,Neapolitan Mastiff,293.73,29/08/2010,none,Medium,no
Obsborne Road Pet Store,Basenji,224.27,13/10/2010,none,Large,yes
]
我的预期输出是
Solly's Pet Store
Giant Schnauzer
£176.43
Wed Jul 07 00:00:00 BST 2010
none
Medium
no.
这是我的宠物类(class):
public class Pet {
private String shop;
private String type;
private double price;
private Date dateAquired;
private String notes;
private String size;
private String neutered;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getNeutered() {
return neutered;
}
public void setNeutered(String neutered) {
this.neutered = neutered;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getShop() {
return shop;
}
public void setShop(String shop) {
this.shop = shop;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getDateAquired() {
return dateAquired;
}
public void setDateAquired(Date dateAquired) {
this.dateAquired = dateAquired;
}
public Pet(String pShop, double pPrice){
this.shop = pShop;
this.price = pPrice;
}
public Pet(String pShop, String pType, double pPrice, Date pDateAcquired, String pNotes, String pSize, String pNeutered){
this.shop = pShop;
this.type = pType;
this.price = pPrice;
this.dateAquired = pDateAcquired;
this.notes = pNotes;
this.size = pSize;
this.neutered = pNeutered;
}
@Override
public String toString(){
return getShop()+"\n"
+getType()+"\n"
+"£"+getPrice()+"\n"
+getDateAquired()+"\n"
+getNotes()+"\n"
+getSize()+"\n"
+getNeutered()+"\n";
}
}
这是file它正在读取。
最佳答案
当你这样做时:
String notes =petReader.nextLine();
它读取整行,直到遇到 \n
,其中包含字符串:
,none,Medium,no
该行的其余部分已被读取。
然后,在接下来的两行中,您再次使用 nextLine()
,因此,它再次读取完整的行,从而获得该输出。
您必须使用:
String notes =petReader.next();
String size =petReader.next();
String neutered =petReader.next();
关于java - 为什么 useDelimiter 不工作 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36646577/