以下是我所拥有的内容:
public void readFile(String fileToOpen) {
File myFile = new File(fileToOpen);
try {
Scanner inFile = new Scanner(myFile);
while (inFile.hasNext()) {
String input = inFile.nextLine();
String [] readString = input.split(",");
for (int i = 0; i < readString.length; i++) {
readString[i].trim();
}
System.out.println(readString[0] + readString[1] + readString[2] + readString[3] + readString[4] + readString[5]);
Point myPoint = new Point(Integer.parseInt(readString[1]), Integer.parseInt(readString[2]));
if (readString[0].toLowerCase().equals("man")) {
Man myMan = new Man(myPoint, Integer.parseInt(readString[3]), Integer.parseInt(readString[4]), readString[5]);
this.myList.add(myMan);
} else if (readString[0].toLowerCase().equals("woman")) {
Woman myWoman = new Woman(myPoint, Integer.parseInt(readString[3]), Integer.parseInt(readString[4]), readString[5]);
this.myList.add(myWoman);
} else {
inFile.close();
throw new IllegalArgumentException();
}
}
inFile.close();
}
我知道这不是完美的,我还在学习。然而,在这里应该可以使用trim()函数...
我的输入文件是:
man, 300, 200, 3, 2, Bill
如果我把修剪过的字符串拼接在一起,应该得到:
man30020032Bill
但是我得到了以下结果:
man 300 200 3 2 Bill
我不知道为什么。有人可以帮忙吗?