我正在开发一个程序,需要将导入的文件放入arraylist并将其拆分为:
00000000 100 80 100 20 20 40 50
12345687 98 67 88 20 19 37 39
89763465 67 79 90 18 16 35 44
我想将第一个数字作为一个列表阅读,依此类推。
public void studentData() throws IOException {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
System.out.println("Enter a file name: ");
String filename = scanner.nextLine().trim();
filename.equalsIgnoreCase(filename);
File f = new File(filename);
Scanner file = new Scanner(f);
String fileText = file.nextLine();
while (file.hasNext()){
list.add(file.next());
}
file.close();
绝对没有打印出来。我希望得到积极的反馈。谢谢!
答案 0 :(得分:0)
您的代码正常运行,
片刻:
如果您的文件不是以\n
开头
然后删除
String fileText = file.nextLine();
并添加:
for (String string : list) {
System.out.println(string);
}
最后,输出你的值。
答案 1 :(得分:0)
我想将第一个数字作为一个列表阅读,依此类推。
您可以将每行数字存储在您定义的列表中,如下所示:
Scanner scanner = new Scanner(System.in);
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
System.out.println("Enter a file name: ");
String filename = scanner.nextLine().trim();
filename.equalsIgnoreCase(filename); // dose't do anything?!!!!
File f = new File(filename);
Scanner file = new Scanner(f);
//String fileText = file.nextLine(); this line removed the first line of txt
while (file.hasNext()) {
list.add(new ArrayList(Arrays.asList(file.nextLine().replaceAll("\\D+",",").split(",")))); // this line is what you need
}
file.close();
我希望这是你的需求