【问题标题】:How to store a line in an Array?如何在数组中存储一行?
【发布时间】: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

【问题讨论】:

    标签: java arrays oop


    【解决方案1】:

    您的代码将循环获取数组的长度,该长度在此 sn-p 中硬编码为:50 个插槽。

    您的文件包含少于 50 个航班,因此会出现此错误。

    我会就如何做这些事情提供建议,但您的问题不遗余力地试图让我们印象深刻,这种将这些数据存储在字符串数组中的愚蠢模型是如何需要的,所以我猜是这样。 . 必需的。

    您说这是必需的,而您的代码显示有 50 个航班。想办法事先知道有多少航班,或者安排 50 个航班在文件中,或者扩展问题域来解释当文件包含少于 50 个航班时应该做什么(或者就此而言,什么如果有超过 50 个航班,应该会发生)。

    注意:如果您的答案是:数组应该与我的文本文件中的飞行线路一样大,那么您需要 ArrayList,而不是 String[]。请注意,如果您在其他地方需要,ArrayList 可以很简单地为您提供 String[]。如果这也不行,那么,Java 库是开源的。看看 ArrayList 是如何工作的,因为您将编写它的克隆。

    【讨论】:

    • 感谢您阅读我的问题并帮助解决数组大小问题。因为只有 30 行,所以我将其更改为 30 个插槽。但是,我仍然遇到同样的错误。我并没有试图给任何人留下深刻印象,因为我对 Java 没有太多经验,这实际上是我在其中的第一个程序。 ArrayList 不是我可以使用的东西,因为我的教授告诉我们不要使用,我们必须手动操作。
    【解决方案2】:

    您是否知道您有 2 个嵌套循环: 内部 for 循环将迭代 50 次(或在您的注释更改后 30 次)并读取整个文件。 您只调用一次 hasNext() 。您必须更改代码,以便在每次调用 scanner.nextLine() 之前调用 scanner.hasNext(),以确保还有一行。

    您遇到错误是因为您的文件只有 29 行。 如果您知道文件中的最大行数,最好依靠scanner.hasNext() 逻辑:

    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];
        int i=0;
        scanner.nextLine(): // skip header line
        while(scanner.hasNext())
        {
            flights[i] = scanFile.nextLine();
        }
    }
    

    【讨论】:

    • 非常感谢!我知道嵌套循环,但由于某种原因我没有注意到它们
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2021-08-26
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多