【问题标题】:Java - Transferring Data from txt file into arraysJava - 将数据从 txt 文件传输到数组
【发布时间】:2019-11-03 22:15:28
【问题描述】:

我目前正在尝试开发一个程序,该程序使用学生 ID 和 GPA(取自 txt 文件)并使用它们来做许多其他事情,例如根据 GPA 范围将学生分为 8 个类别中的 1 个,制作直方图每个组的学生,并按 GPA 对学生进行排名。但是,我需要做的第一件事是将学生 ID 和 GPA 转移到两个单独的数组中。

我知道创建数组的语法如下:

elementType[] arrayRefVar = new elementType[arraySize]

但是,我仍然不知道如何将从文件中读取的数据传递到两个单独的数组中。我要从txt文件中读取数据的代码如下:

public static void main(String[] args) throws Exception  // files requires exception handling
{
    String snum;     
    double gpa;
    Scanner gpadata = new Scanner(new File("studentdata.txt"));

    while (gpadata.hasNext()) // loop until you reach the end of the file 
    {
        snum = gpadata.next(); // reads the student's id number
        gpa = gpadata.nextDouble(); // read the student's gpa

        System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window

    }
}

所以我的问题是:如何将这些信息传递到两个单独的数组中?如果我的问题难以理解,我深表歉意,我对编程非常陌生。我已经被这个程序难倒了很长时间,任何帮助将不胜感激!谢谢。

【问题讨论】:

    标签: java arrays file java.util.scanner


    【解决方案1】:

    您可以在while循环之前创建两个数组,然后将循环内的每个元素添加到每个数组中。但是这种方法有一个问题:我们不知道值的数量,因此我们不能为此创建一个固定大小的数组。我建议改用ArrayList,它可以根据需要增长。像这样的:

    public static void main(String[] args) throws FileNotFoundException {
    
        Scanner gpadata = new Scanner(new File("studentdata.txt"));
        List<String> IDs = new ArrayList<>();
        List<Double> GPAs = new ArrayList<>();
        while (gpadata.hasNext()) // loop until you reach the end of the file
        {
            String snum = gpadata.next(); // reads the student's id number
            double gpa = gpadata.nextDouble(); // read the student's gpa
    
            IDs.add(snum);
            GPAs.add(gpa);
    
            System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window
        }
        // Use IDs and GPAs Lists for other calculations
    }
    

    使用Map 将 GPA 与学生 ID“配对”的更好方法。

    编辑:

    在您澄清最大记录数永远不会超过 1000 之后,我修改了我的解决方案以使用数组而不是列表。我没有更改变量名称,因此您可以轻松比较解决方案。

    public static void main(String[] args) throws FileNotFoundException {
    
        Scanner gpadata = new Scanner(new File("studentdata.txt"));
        String[] IDs = new String[1000];
        double[] GPAs = new double[1000];
        int counter = 0;
    
        while (gpadata.hasNext()) // loop until you reach the end of the file
        {
            String snum = gpadata.next(); // reads the student's id number
            double gpa = gpadata.nextDouble(); // read the student's gpa
    
            IDs[counter] = snum;
            GPAs[counter] = gpa;
    
            System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window
            counter++;
        }
        // Use IDs and GPAs Lists for other calculations
    }
    

    请注意,我们需要一个 counter(又名索引)变量来寻址数组槽。

    【讨论】:

    • 我应该澄清一下,程序声明你可以假设文件永远不会有超过 1000 个 S 编号和 GPA
    • 有了这些信息,我为我的答案添加了另一个解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    相关资源
    最近更新 更多