When printing an array elements are not shown

Asked

Viewed 290 times

2

I am trying to solve a problem where I have to read the lines of a file . txt and separate the numbers of this line and save in a vector.

public static void main(String[] args) throws IOException {
        FileReader arq = new FileReader("Teste.txt");
        BufferedReader lerArq = new BufferedReader(arq);
        
        int[][] num = new int[20][20];
        String lerLinha = "";
        String[] linha = new String[30];
        
        lerLinha = lerArq.readLine();
        System.out.println(lerLinha);
        linha = lerLinha.split(" ");
        System.out.print(linha);
}

Running this code it returns:

61 57 18 -73 74 -19 15 -70 -16 -8 -80 -6 2 -85 8 22 73 -44 34 1
[Ljava.lang.String;@15db9742

I wanted to know how to solve this problem with the variable linha who is returning this java.lang.String;@15db9742.

2 answers

3


When you encounter a problem like this look in the documentation how that method works. In fact, do not use a method without reading and understanding all its documentation and what is related.

Documentation of split().

And there it says that the return of this method is a array of strings. So there are several elements in the object. Which of the elements do you want to print? He’s not specifying, he’s talking to print the whole object and he doesn’t know how to do it, he just knows to take the reference of the object, which is what was printed.

So either you tell us what the element of array who wants to print (linha.get(0)) or makes a loop to take all the elements and print each of them, something like this:

for (String item : linha) System.out.print(item);

This variable item will be a String, because all elements of array are that kind, and it’s a guy he knows how to print perfectly.

0

linha is an array of String's, and when you print an array with System.out.println(array), internally is called the method toString() of the same. Only that arrays inherit this method of Object, whose code is:

// método toString de java.lang.Object
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

That is, it returns the class name and the hashcode of the same. In the case of arrays, the documentation explains that the amount of [ represents the dimension of the array (in this case, it is one-dimensional, but if it were an array of arrays, for example, it would be [[), and L is the prefix that indicates that the array elements are of a class (and then comes the class name). That’s why the output starts with [Ljava.lang.String.

That is, printing an array directly will not show the elements of it.

If you want to print the elements, you can make a loop as already suggested the another answer, or you can use Arrays.toString:

System.out.println(Arrays.toString(linha)); // [61, 57, 18, -73, 74, -19, 15, -70, -16, -8, -80, -6, 2, -85, 8, 22, 73, -44, 34, 1]

The difference is that Arrays.toString returns a fixed format (comma-separated elements, bounded by square brackets). For the purpose of debug may be enough, but if you want a different format, there’s no way, you need to make a loop and print in the desired format.

Another detail is that Arrays.toString internally calls the method toString() of each element. That is, if the elements are of a class that does not implement toString(), the exit will be a lot of nomedaclasse@hashcode. As in your specific case the array contains String's, this problem does not occur.

And in the specific case of an array of String's, you can also use String.join (available from Java 8):

System.out.println(String.join(" | ", linha)); // 61 | 57 | 18 | -73 | 74 | -19 | 15 | -70 | -16 | -8 | -80 | -6 | 2 | -85 | 8 | 22 | 73 | -44 | 34 | 1

The join joins all array values into a single String, and you can choose the tab (used " | " as an example, but it could be any other).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.