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.