【问题标题】:How to read data from a text file into arrays in JavaJava如何将文本文件中的数据读入数组
【发布时间】:2013-04-11 19:16:16
【问题描述】:

我在编程作业上遇到问题。我需要从 txt 文件中读取数据并将其存储在并行数组中。 txt文件内容的格式如下:

Line1: Stringwith466numbers
Line2: String with a few words
Line3(int): 4
Line4: Stringwith4643numbers
Line5: String with another few words
Line6(int): 9

注意:“Line1:”、“Line2:”等仅用于显示目的,实际上不在 txt 文件中。

如您所见,它以三人一组的形式出现。 txt 文件的每个条目是三行,两个字符串和一个 int。

我想将第一行读入一个数组,第二行读入另一个,第三行读入一个 int 数组。然后将第四行添加到第一个数组中,将第 5 行添加到第二个数组中,将第 6 行添加到第三个数组中。

我已尝试为此编写代码,但无法正常工作:

//Create Parallel Arrays
String[] moduleCodes = new String[3];
String[] moduleNames = new String[3];
int[] numberOfStudents = new int[3];

String fileName = "myfile.txt";


readFileContent(fileName, moduleCodes, moduleNames, numberOfStudents);

private static void readFileContent(String fileName, String[] moduleCodes, String[] moduleNames, int[] numberOfStudents) throws FileNotFoundException {

        // Create File Object 
        File file = new File(fileName);

        if (file.exists())
        {

            Scanner scan = new Scanner(file);
            int counter = 0;

            while(scan.hasNext())
            {


                String code = scan.next();
                String moduleName = scan.next();
                int totalPurchase = scan.nextInt();

                moduleCodes[counter] = code;
                moduleNames[counter] = moduleName;
                numberOfStudents[counter] = totalPurchase;

                counter++; 


            }

        }

    }

上面的代码不能正常工作。当我尝试打印出数组的一个元素时。它为字符串数组返回 null,为 int 数组返回 0,这表明读取数据的代码不起作用。

非常感谢任何建议或指导,因为在这一点上它变得令人沮丧。

【问题讨论】:

    标签: java arrays file


    【解决方案1】:

    只有null 被打印的事实表明该文件不存在或为空(如果打印正确的话)。

    最好进行一些检查以确保一切正常:

    if (!file.exists())
      System.out.println("The file " + fileName + " doesn't exist!");
    

    或者您实际上可以跳过上面的内容,并在您的代码中取出 if (file.exists()) 行并让 FileNotFoundException 被抛出。

    另一个问题是next 按空格分割内容(默认情况下),问题是第二行有空格。

    nextLine 应该可以工作:

    String code = scan.nextLine();
    String moduleName = scan.nextLine();
    int totalPurchase = Integer.parseInt(scan.nextLine());
    

    或者,更改分隔符也应该有效:(使用您的代码)

    scan.useDelimiter("\\r?\\n");
    

    【讨论】:

    • 感谢您的回复,我添加了支票并打印了错误。我的 txt 文件位于 eclpise 的项目文件夹中,所以我不明白为什么它不起作用。
    • @JavaCoder Two related 可能对您有所帮助的问题。
    • 还是不行,一定是找文件有问题,所有的代码建议都试过了,都没有成功。
    • @JavaCoder 我对 Eclipse 及其可能的问题不太熟悉,我只想指出,正如其中一个问题所述,它必须位于项目的根目录中(在src 目录所在的同一目录中,但不在src 目录本身)。无论哪种方式,将absolute path 用于fileName 应该可以工作。
    • 它现在可以从文件中读取,它给出了一个错误,说 int 值的输入不匹配所以我将它更改为一个字符串只是为了让它工作。 (当然我最终需要让它与 int 一起工作)。现在它将字符串读入数组,但是每行只打印一个单词。如果模块名称是“计算机体系结构”。它实际上只在“计算机”中读取。
    【解决方案2】:
      String code = scan.nextLine();
      String moduleName = scan.nextLine();
      int totalPurchase = scan.nextInt();
      scan.nextLine()
    

    这将在读取int 后将扫描仪移动到正确的位置。

    【讨论】:

    • 感谢您的回复,我将此添加到我的代码中,但它似乎没有改变任何东西。当我尝试打印数组的元素时仍然输出“null”或“0”。
    【解决方案3】:

    你正在阅读行,所以试试这个:

    while(scan.hasNextLine()){
        String code = scan.nextLine();
        String moduleName = scan.nextLine();
        int totalPurchase = Integer.pasreInt(scan.nextLine().trim());
    
        moduleCodes[counter] = code;
        moduleNames[counter] = moduleName;
        numberOfStudents[counter] = totalPurchase;
        counter++; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2012-10-20
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多