简体   繁体   English

将.txt文件中的数据存储到数组中并创建等级

[英]Storing data from .txt file into array and creating ranks

I'm trying to use data from a txt file to populate an array. 我正在尝试使用txt文件中的数据填充数组。 Essentially, I don't understand what the structure of the loop code needs to be. 本质上,我不了解循环代码的结构。 I understand that the index needs to be treated like an int variable in the loop, but I don't know what that looks like. 我知道索引在循环中需要像int变量一样对待,但是我不知道它是什么样。

This is what I have so far and it's very wrong, 这是我到目前为止所拥有的,这是非常错误的,

public class ReadTextFileUsingScanner
{

    public static void main(String[] args) throws Exception 
    {
        String[] snumArray;
        snumArray = new String[1000];

        double[] gpaArray;
        gpaArray = new double[1000];

        int rank = 0;
        double currentGPA = 4.1;



        Scanner gpadata = new Scanner(new File("studentdata.txt"));

        while (gpadata.hasNext())
        {
            snumArray[0] = gpadata.next();
            gpaArray[0] = gpadata.nextDouble();

            if (currentGPA > gpaArray[0])
            {
                rank++;
            }

            System.out.println(snumArray[0] + "\t" + gpaArray[0] + "\t" + rank);

        }
    }


}

I'm only a couple months into java programming and I've really hit a brick wall with this. 我只用了几个月的Java编程时间,就真的碰到了这个难题。 I need my output to look like this: 我需要我的输出看起来像这样:

S316542                3.45        27
S29463                  1.42        801
S969870                2.75        T64 with 8 others

And I can't change the order of the students so I can't use sort to create the rank. 而且我无法更改学生的顺序,因此无法使用排序来创建排名。

All I really want to know though, is how do I populate the index with data from the .txt file? 我真正想知道的是,如何用.txt文件中的数据填充索引? The way I'm doing it now (is stupid, I know) is only ever populating the [0] index then printing, then replacing the data at the [0] index. 我现在的操作方式(很愚蠢,我知道)只是填充[0]索引,然后打印,然后替换[0]索引处的数据。 That's not right. 那是不对的。 I've spent probably a good 5 hours searching the internet for ANYTHING like this and there are TONS of similar questions out there but I can't understand any of the answers at all. 我大概花了5个小时在互联网上搜索类似的内容,并且有大量类似的问题,但是我根本无法理解任何答案。 Can someone just show me what that code structure would be? 有人可以告诉我该代码结构是什么吗?

You can just make an int line variable and iterate at the end of the loop 您可以制作一个int line变量并在循环结束时进行迭代

Then, you want to check lines from the Scanner; 然后,您要检查扫描仪中的线; .next() only reads to the next whitespace. .next()仅读取下一个空格。

   Scanner gpadata = new Scanner(new File("studentdata.txt"));
   int line = 0; // Add this
   while (gpadata.hasNextLine()) {
        String[] columns = gpadata.nextLine().split("\t");

        snumArray[line] = columns[0];
        gpaArray[line] = Double.parseDouble(columns[1]);

        if (currentGPA > gpaArray[line]) {
            rank++;
        }

        System.out.println(snumArray[line] + "\t" + gpaArray[line] + "\t" + rank);

        line++; // Add this
    }
}

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

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