0

I wish to store the contents in text file in an array. Here are the data of the txt file:

 0.8585781857237149 0.27817454182457335 -0.8050499953993335 

0.6370714882668496  0.2972334455862271  -0.03239256370254662    

-0.27150466294617615    0.6458147357741209  -0.8755197569879973         



0.8714523367008264  0.5051711395439467  0.7632793840501568      

0.9722198583553305  -0.6540230961515898 0.5498519669064881  

-0.1289712393377327 0.5729094349133539  -0.32452314324200193    

I have tried to execute this code:

FileInputStream in = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));

                    while ((line = br.readLine()) != null) {                        

                        for (z=0; z<2; z++){    
                            for (y=0; y<9; y++){
                                for (x=0; x<5; x++){
                                    filearray[x][y][z]=br.readLine();
                                }
                            }
                        }

                        System.out.println(Arrays.toString(filearray));
                    }

but the output gave me an error with such error message:

[[[Ljava.lang.String;@2a3046da, [[Ljava.lang.String;@2a098129, [[Ljava.lang.String;@198e2867, [[Ljava.lang.String;@12f40c25, [[Ljava.lang.String;@3ada9e37]

Is there any mistake made in my code? Thanks in advance.

gonzalloe
  • 313
  • 3
  • 7
  • 22
  • are you sure that is an error message? I believe it's actually the output you're printing – Shinra tensei Sep 07 '17 at 10:49
  • 1
    "_the output gave me an error_" That is not an error, that is valid `toString()` output from an array. – takendarkk Sep 07 '17 at 10:51
  • Try printing the specific object from that array. If you try printing a whole array, it always provides a HashCode to it. Not the direct values. Try System.out.println(Arrays.toString(filearray[x][y][z])); in your for loop. – Procrastinator Sep 07 '17 at 10:54
  • do you really want an entire line per array element? you have a bunch of `readline`s but no parsing of any kind. – Les Sep 07 '17 at 10:55
  • @Les I want one `double` value in one array element. So, which part I should amend? Or can I have a clearer coding explanation? Sorry but im really new to Java. – gonzalloe Sep 07 '17 at 11:19

1 Answers1

0

Arrays.toString() is a function that returns you the contents of an array. Guess what, you have a 3D array, so it will print the arrays inside the 1st one. You should loop over the arrays using that function only in the "3rd dimension".

To be more clear:

for (x=0; x<5; z++){    
    for (y=0; y<9; y++){
        System.out.println(Arrays.toString(filearray[x][y]));
    }
}

You have to modify it to print them in the order you prefer but I think you can understand the way to do it.

Shinra tensei
  • 1,283
  • 9
  • 21
  • I've tried this`System.out.println(Arrays.toString(filearray[x][y][z]));` in the for loop. Unfortunately, the method `toString`(long[]) in the type Arrays is not applicable for the arguments (String). Are the contents all stored in array? – gonzalloe Sep 07 '17 at 11:12
  • read my answer again. I told you to try it with `System.out.println(Arrays.toString(filearray[x][y]));`, because the content of `filearray[x][y]` is an array. If you use `System.out.println(Arrays.toString(filearray[x][y][z]));`, you're trying to use an `Arrays` function on a `String` element. – Shinra tensei Sep 07 '17 at 11:25