所以我的程序读取.txt文件,我正在使用.useDelimeter来分隔宠物并将它们放入arraylist中:
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.
}
这是输出。事实上它在最后两个变量中得到了下一个petshops:
[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.
这是我的Pet课程:
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。
答案 0 :(得分:2)
当你这样做时:
String notes =petReader.nextLine();
它会读取整行,直到遇到\n
,其中包含字符串:
,无,中等,没有
该行的其余部分已被阅读。
然后,在接下来的两行中,您再次使用nextLine()
,因此,它再次读取完整的行,从而获得该输出。
您必须使用:
String notes =petReader.next();
String size =petReader.next();
String neutered =petReader.next();