简体   繁体   中英

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. 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.

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. (one person per row, with 3 collumns for their 3 strings). 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? 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.

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.

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.

Remove bracket before line Selection_Sort selection = new Selection_Sort(); Change the variable i to j and code is as below :

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

Then the variable i has already been used so you need to use a different variable name either of those places where its used

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

Lastly use main instead of main18k so the application has a valid entry point

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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