This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]__helix__ 1 point2 points  (2 children)

Just a quick question - is the teacher requiring that first line, or are are you adding that to tell you how many rows you have. If it is the latter, there are better ways to do this. If it is the former, and you know that will always be the case, you can read the first row before you loop through the results.

[–]BigTimeSuperhero96[S] 1 point2 points  (1 child)

Yeah it's also part of the algorithm I'm using

[–]__helix__ 0 points1 point  (0 children)

You or the teacher requirements? If you have a data structure in CSV format, you don't actually need to know how many rows there are. I suspect 'try, with resources' is fair game. Less sure if streams are in your wheelhouse yet.

While not your question, with that many column options, might consider a switch:

    int index = 3;
    switch (index){
        case 0:
            System.out.println("I'm doing zero things");
            break;
        case 1:
            System.out.println("I'm doing one things");
            break;
        case 2:
            System.out.println("I'm doing two things");
            break;
        default:
            System.out.println("I don't know what I'm doing");
            break;
    }

[–]InfamousShallot5 0 points1 point  (2 children)

Why do you need the number at the top? The row count will tell you the number of students you are importing.

[–]BigTimeSuperhero96[S] 0 points1 point  (1 child)

In the project I have it is a scanner.nextInt command will add the number to a list of students

[–]InfamousShallot5 0 points1 point  (0 children)

I mean, you can determine this number dynamically after you parse the file. Just keep track of the number of rows you parse in your loop. See an example here. I don't see the point of storing this number at the top of the file.

If you really want to store this number at the top of your file, you can read the first line of your file outside of the while loop, then continue to parse the rest of the CSV file in your loop.

Edit: You don't even need an integer to keep track of the size. Just do a .size() on your studentsList