我有一个逗号分隔的文本文件,其信息格式如下:
名字,姓氏,餐1,餐2,餐3,餐4 ....每个新学生都在新的一行。
我有以下学生对象。
public class Student {
private String first = null;
private String last = null;
public Student (String first, String last){
this.first = first;
this.last = last;
}
我需要一个从另一个类使用的方法来填充学生对象数组。
我不确定如何使用扫描仪执行此操作,因为我只需要每行的前两行,任何为我指明正确方向的帮助将非常感激。
~谢谢!
最佳答案
try {
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String array[] = line.split(",");
Student student = new Student (array[0],array[1]);
-------------------------
-------------------------
System.out.println("FirstName:"+ array[0]);
System.out.println("LastName:"+ array[1]);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
关于java - 将文本文件扫描到对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023906/