0

how to do extract a string number from a csv file and put it in an int array. I tried to do the following but I am not getting what I need. The csv file contains the name and grade separated by comma. In the code below when I print values I get the numbers but as soon as I try to put them in the int array I get a really weird output. CSV file content:

Betty Mason, 53
Lillian Black,3
Steven Gonzalez , 27
Dorothy Sullivan,80
Bobby Cooper, 68

Code

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Grade {
    public static void main(String[] args) throws FileNotFoundException {
    Scanner s1 = new Scanner(new File("grades.csv"));

    int numberofLines = 0;
    while (s1.hasNext()) {
        String line = s1.nextLine();
        numberofLines++;
    }

    int[] myArray = new int[numberofLines];

    Scanner s2 = new Scanner(new File("grades.csv"));
    while (s2.hasNext()) {
        String line = s2.nextLine();
        String[] values = line.split(",");
        String tempValues = values[1].trim();
        for (int i = 0; i < numberofLines; i++) {
            myArray[i] = Integer.parseInt(tempValues);
        }

        System.out.println(myArray);

    }

}

}

When I do System.out.println(tempValues); I get

53
3
27
80
68

And When I do System.out.println(myArray); I get

I@3d4eac69
[I@3d4eac69
[I@3d4eac69
[I@3d4eac69
[I@3d4eac69

How do I fix this problem? Thank you.

1 Answer 1

1

To print the content of the array and not its address, use Arrays.toString:

System.out.println(Arrays.toString(myArray));
Sign up to request clarification or add additional context in comments.

2 Comments

It gave me an error saying: cannot find symbol symbol: variable Arrays location: class Grade
Nevermind I did this and it showed up import java.util.Arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.