这是我的 portefeuille 应该包含所有读取“woningen”
//read file
public static Portefeuille read(String infile) {
Portefeuille result = new Portefeuille();
FileReader fileString;
try{
fileString = new FileReader(infile);
Scanner sc = new Scanner(fileString);
int amount = sc.nextInt();
for(int i = 0; i < amount; i++)
result.voegToe(readWoning.read(sc));
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
return result;
}
这使用 readWoning.read(sc) 这是:
public class readWoning {
public static Woning read(Scanner ac) {
String toestand = ac.next();
Adres adres = Adres.read(ac);
int kamers = ac.nextInt();
ac.next(); String aard = ac.next();
int price = ac.nextInt();
System.out.println(toestand + " " + adres + " " + kamers + " " + price);
if("huurprijs".equals(aard)) {
return new HuurWoning(adres,kamers,price,toestand);
}
return new KoopWoning(adres,kamers,price,toestand);
}
}
这使用 Adres.read(ac); 这是:
public static Adres read(Scanner sc) {
sc.nextLine();
String street = sc.next();
String number = sc.next();
String code = sc.next();
String place = sc.next();
return new Adres(street, number, code, place);
}
我要读入的文件是:
3
TE KOOP:
Emmalaan 23
3051JC Rotterdam
7 kamers
vraagprijs 300000
VERKOCHT:
Emmalaan 25
3051JC Rotterdam
5 kamers
koopprijs 280000
TE HUUR:
Javastraat 88
4078KB Eindhoven
3 kamers
huurprijs 500
其中第一个数字表示文件中描述了多少个 woningen。第一行显示了中奖发生的“toestand”(状态),可能是:(已售、已租等)。第 2 行和第 3 行表示中奖地址。第 4 行表示中奖内的房间数量 最后一行表示中奖的价格。
现在我的问题是:
由于 toestand,我无法让 Java 正确读取我的文件:“TE KOOP:”我在 readWoning 中使用了 String toestand = ac.nextLine() 但它只输出一些空格,而 String toestand = ac.next(); 仅输出“TE”如果我的问题不够具体,请告诉我。
我得到的输出:
TE Emmalaan 23, 3051JC Rotterdam 7 300000
VERKOCHT: Emmalaan 25, 3051JC Rotterdam 5 280000
TE Javastraat 88, 4078KB Eindhoven 3 500
null
null
null
我想要的输出:
TE KOOP: Emmalaan 23, 3051JC Rotterdam 7 300000
VERKOCHT: Emmalaan 25, 3051JC Rotterdam 5 280000
TE HUUR: Javastraat 88, 4078KB Eindhoven 3 500
提前致谢,