简体   繁体   English

Java:为什么无法从csv文件输出排序后的值?

[英]Java: How come I can't output my sorted values from a csv file?

I have a program that reads from a csv file full of peoples last names, first names, and birth years, assigns them into a special class array, and then gets sorted according to their last name. 我有一个程序,该程序从充满人名,姓氏和出生年月的csv文件中读取内容,并将其分配到特殊的类数组中,然后根据其姓氏进行排序。 I believe that my code is working, so all I have to do to verify this is output the list and see if indeed all of the people were sorted by their last name. 我相信我的代码可以正常工作,因此我要做的就是验证列表输出的内容,看看是否所有的人都按姓氏排序。 However, I am having trouble finding the right syntax to do this. 但是,我很难找到正确的语法来做到这一点。 Here is the code of my Main.java, where I think the issue must be. 这是我的Main.java的代码,我认为这个问题一定存在。

package project_1_sorting;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class Main 

{

    public static void main(String[] args) throws IOException {
        // open file input stream
        BufferedReader reader = new BufferedReader(new FileReader(
                "C:\\Users\\Owner\\Desktop\\Data 18k.csv")); // double check where this is trying to read it from

        // read file line by line
        String line = null;
        Scanner scanner = null;
        int index = 0;
        Human[] pplArray = new Human[18000];
                int i = 0;
        while ((line = reader.readLine()) != null) {
            Human ppl = new Human();
            scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                String data = scanner.next();
                if (index == 0)
                    ppl.setLastName(data);
                else if (index == 1)
                    ppl.setFirstName(data);
                else if (index == 2)
                    ppl.setBirthYear(data);
                else
                    System.out.println("invalid data::" + data);
                index++;
            }
                        ppl.setKey(0); //change this for later things, you can use loop
                        ppl.setOrder(0); //change this to 1 if you want to invert the list of people
            index = 0;
            pplArray[i] = ppl;
                        i++;
                        System.out.println(pplArray);
        }
        //close reader
        reader.close();

        System.out.println(pplArray); // create


           Selection_Sort selection = new Selection_Sort();   


            for (int j = 0; j < 18000; j++)
     {
         System.out.println(pplArray[j]);
     }
    }

}

So I was expecting this to output a giant list of all of my people from the csv file(ordered), with all of their info in the same format as they originally were, right. 因此,我期望这会从csv文件(排序的)中输出我所有人员的庞大列表,其所有信息的格式与原始格式相同,对。 (one person per row, with 3 collumns for their 3 strings). (每行一个人,三个字符串的3个列)。 However this is what I got instead: 但是,这是我得到的:

run:
Test
17
true
0.142857
BUILD SUCCESSFUL (total time: 0 seconds)

I am not sure what the meaning of this is. 我不确定这是什么意思。 It would seem that its doing something completely unrelated to what I am trying to do. 看来它所做的事情与我正在尝试的工作完全无关。 This is the only project that I have open in NetBeans, so it must be generated from my functions, right? 这是我在NetBeans中打开的唯一项目,因此必须从我的函数中生成,对吗? If anyone knows what this is all about, please let me know. 如果有人知道这是怎么回事,请告诉我。 If there is nothing else wrong with this Main.java, I can post my other .java files. 如果此Main.java没有其他问题,则可以发布其他.java文件。

One thing I did notice was that, even when I commented out my selection sort function call, and all of the printline commands in this .java file, the same output was displayed on my screen. 我确实注意到的一件事是,即使当我注释掉选择排序函数调用以及此.java文件中的所有printline命令时,我的屏幕上仍显示相同的输出。

Please let me know what you think. 请让我知道你的想法。

You have not close the bracket properly.Also variable i is used twice in the main method.So change the variable name. 您没有正确合上支架。变量i在main方法中也被使用了两次,因此请更改变量名称。

Remove bracket before line Selection_Sort selection = new Selection_Sort(); Selection_Sort selection = new Selection_Sort();行之前删除括号Selection_Sort selection = new Selection_Sort(); Change the variable i to j and code is as below : 将变量i更改为j,代码如下:

for (int j = 0; j < 18000; j++)
     {
         System.out.println(pplArray[j]);
     }

You have a few issues 你有几个问题

The statements 陈述

Selection_Sort selection = new Selection_Sort(); 
for (int i = 0; i < 18000; i++)
{
    System.out.println(pplArray[i]);
}

should be in the main18k method rather than the class block 应该在main18k方法中,而不是在类块中

Then the variable i has already been used so you need to use a different variable name either of those places where its used 那么变量i已经被使用过,因此您需要在使用变量的地方使用不同的变量名

for (int j = 0; j < 18000; j++)

Lastly use main instead of main18k so the application has a valid entry point 最后使用main代替main18k以便应用程序具有有效的入口点

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

相关问题 如何使用 Java 创建 csv 文件中具有相同行名的所有值的列表 - How can I use Java to create a list of all the values from csv file that have the same row name 如何从Java中的控制台从输出文件中获取特定的xml标记 - How can I get specific xml tag from output file from my console in java 为什么无法正确显示这些值? - How come I can't get the values to display properly? 如何使用文件中的特定列对排名列表进行排序并打印排序后的整个文件? - How can I sort a Ranking list using a specific column from a file and print the whole file sorted?Java 我怎么不能在Java中对数组进行递减计数呢? - How come I can't count downwards on an array in Java? 如何将我的数据从 csv 文件获取到 DataProvider - How can I get my data from csv file to DataProvider 如何将数据从数据库导出到csv文件? - How can I export datas from my database to a csv file? 如何查看Jar文件的输出? - How can I see the output from my Jar file? 如何将 CSV 文件和 output 中的数字与 Java 中的 TXT 文件相加? - How to sum numbers From a CSV File and output it to TXT File In Java? 我无法阻止 null 出现在我的 output 中,用于我在 java 中的一维数组 - I can't stop null from showing up in my output for my 1d array in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM