【发布时间】:2020-09-13 20:46:17
【问题描述】:
我一直在尝试将 ling 存储在数组中的文本文件中,但我对此无能为力,而且我无法使用 ArrayList,这意味着它需要手动完成。这是我目前的代码。顺便说一句,文本文件的第一行(标题)不应该包含在数组中。
public class Main {
public static void main (String[] Args) throws IOException {
/*Getting the file and going through each line*/
File planeFile = new File("plane.txt");
Scanner scanFile = new Scanner(planeFile);
/*Array to store the flights in*/
String[] flights = new String[50];
while(scanFile.hasNext()) {
for(int i = 0; i < flights.length; i++) {
flights[i] = scanFile.nextLine();
}
Arrays.toString(flights);
}
}
}
这是我的代码的输出:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Main.main(Main.java:20)
但我需要它将文本文件的每一行存储在一个数组中。这是文件:
ID, Destination, Day, Meal, Rows, NumPerRow
9010, Florida, 3, no, 22, 4
9002, Florida, 1, no, 22, 4
9005, Florida, 1, yes, 32, 5
9013, Florida, 3, yes, 32, 5
9008, Florida, 2, yes, 32, 5
9024, Florida, 6, yes, 32, 5
9016, Florida, 4, yes, 32, 5
9021, Florida, 5, yes, 32, 5
9018, Florida, 5, no, 22, 4
9029, Paris, 7, yes, 32, 5
9026, Bahamas, 7, no, 22, 4
9007, Boston, 2, no, 24, 4
9015, Boston, 4, no, 24, 4
9023, Boston, 6, no, 24, 4
9009, NYC, 2, yes, 32, 5
9012, NYC, 3, yes, 32, 5
9004, NYC, 1, yes, 32, 5
9025, NYC, 6, yes, 32, 5
9017, NYC, 4, yes, 32, 5
9020, NYC, 5, yes, 32, 5
9011, Chicago, 3, no, 24, 4
9019, Chicago, 5, no, 24, 4
9003, Chicago, 1, no, 24, 4
9014, DC, 4, no, 22, 4
9006, DC, 2, no, 22, 4
9022, DC, 6, no, 22, 4
9027, Jamaica, 7, no, 24, 4
9028, London, 7, yes, 32, 5
【问题讨论】: