2

I'm am very much a rookie when it comes to coding. What I'm trying to accomplish is to load grades from a txt file and put them into an existing array.

I have actually solved this. and In the program the array does retain the values. But when I got to print them I get the object reference aka [I@5c647e05 rather than the array. Below is my code.

   public static void main(String[] args)    {
        int[] list = new int[15];
        Scanner in = null; // create a scanner object
        loadGrades(in, list);

        System.out.println(list);
    }

    public static void loadGrades(Scanner in, int[] list) {

        int grades; // int variable
        try { // try            
            in = new Scanner(new FileReader("Proj5Data.txt"));// change filename
        } catch (FileNotFoundException ex) { // catch
            System.out.println("File not found");
            System.exit(1);
        }
        for (int i = 0; i < list.length; i++) {         
                grades = in.nextInt(); // var
                list[i] = grades ;// put the int in the array at the counter value            
        }
        in.close();

    }

Print results as:

[I@5c647e05
BUILD SUCCESSFUL (total time: 0 seconds)
SynGaren NA
  • 21
  • 1
  • 4

4 Answers4

1

When you print an array behind the scenes the method toString() is being called and it doesn't print the content of the array (which is too bad IMO). But you can easily get over it using the library Arrays:

System.out.println(Arrays.toString(list));
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

You can't print the contents of a list like that in Java. You need to loop over the contents of the array and print them individually.

for(int i : list){
  System.out.println(i);
}
Quicksilver002
  • 735
  • 5
  • 12
0

System.out.println(list) uses a .toString() method to resolve what to print. You can either use Arrays.toString(list) or print it one by one.

Mateusz Was
  • 193
  • 10
-1

So All the methods you guys gave me did indeed work. But I ended up just using printf to print them out. Thanks for the help though.

SynGaren NA
  • 21
  • 1
  • 4