简体   繁体   English

Java - 将数据从 txt 文件传输到 arrays

[英]Java - Transferring Data from txt file into arrays

I am currently trying to work on a program that uses student ID's and GPA's (taken from a txt file) and uses these to do numerous other things like categorize the students into 1 of 8 categories based on GPA ranges, make a histogram of the students in each group, and also rank the students by GPA.我目前正在尝试开发一个程序,该程序使用学生 ID 和 GPA(取自 txt 文件)并使用它们来做许多其他事情,例如根据 GPA 范围将学生分为 8 个类别中的 1 个,制作学生的直方图在每个组中,并按 GPA 对学生进行排名。 The first thing I need to do however is transfer the student ID's and GPA's into two separate arrays.然而,我需要做的第一件事是将学生证和 GPA 转移到两个单独的 arrays 中。

I know the syntax for creating an array is as follows:我知道创建数组的语法如下:

elementType[] arrayRefVar = new elementType[arraySize]

However, I still don't know how to pass the data that is read from the file into two separate arrays.但是,我仍然不知道如何将从文件中读取的数据传递到两个单独的 arrays 中。 The code I have to read the data from the txt file is as follows:我必须从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

    }
}

So my question is: how do I pass this information into two separate arrays?所以我的问题是:如何将此信息传递给两个单独的 arrays? I apologize if my question is hard to understand, I am extremely new to programming.如果我的问题难以理解,我深表歉意,我对编程非常陌生。 I have been stumped on this program for a long time now, and any help would be extremely appreciated.我已经被这个程序难倒了很长时间,任何帮助将不胜感激。 Thank you.谢谢你。

You can create two arrays before the while loop, and then add each element inside the loop to each array.您可以在while循环之前创建两个arrays,然后将循环内的每个元素添加到每个数组中。 But there is one problem with this approach: we don't know the number of values, thus we cannot create a fixed-sized array for this.但是这种方法有一个问题:我们不知道值的数量,因此我们不能为此创建一个固定大小的数组。 I suggest to use ArrayList instead, which can grow as needed.我建议改用ArrayList ,它可以根据需要增长。 Something like this:像这样的东西:

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
}

A more better approach to use a Map to "pair" a GPA to a Student ID.使用Map将 GPA 与学生 ID“配对”的更好方法。

Edit:编辑:

After you clarified that the max record number never be more than 1000, I modified my solution to use arrays instead of Lists.在您澄清最大记录数永远不会超过 1000 之后,我修改了我的解决方案以使用 arrays 而不是列表。 I didn't change the variable names so you can compare the solutions easily.我没有更改变量名称,因此您可以轻松比较解决方案。

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
}

Note that we need a counter (aka. index) variable to address the array slots.请注意,我们需要一个counter (又名索引)变量来寻址数组槽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM