0

I have to read data from text.txt file, but I have strange error, my output is: [Ljava.lang.String;@5f0a94c5.

The contents of text.txt file:

test::test.1::test.2
test2::test2.1::test2.2
test3::test3.1::test3.2

The code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class test {
        public static void main(String[] args){
            ArrayList<String> data = new ArrayList<String>();

            try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
            String CurrLine;

            while((CurrLine = br.readLine()) != null) {
                data.add(CurrLine);
            }
            String[] dataArray = new String[data.size()];
            data.toArray(dataArray);
            Arrays.toString(dataArray);
            System.out.println(dataArray);


        } catch(FileNotFoundException ex) {
            System.out.println("FNFE");
        } catch(IOException ex) {
            System.out.println("IOE");
        }
    }
}

2 Answers 2

4

You need to use:

System.out.println(Arrays.toString(dataArray));

In your code, Arrays.toString(dataArray); does nothing as you don't do anything with its returned value.

BTW, as @ZouZou pointed out, you can also print your ArrayList directly:

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

5 Comments

can i use String arr = Arrays.toString(dataArray)); ?
Yes. You can use it and print arr.
@user3104545 Why don't you directly print your list ?System.out.println(data);
Glad I could help. You can accept it if you find it helpful :)
i need wait for 30 secs for this)
1

Your code : System.out.println(dataArray); will output the hashcode value for the object dataArray. Any array in Java does not override equals() method. As a result, when you try to print the value of the array object, java.lang.Object.equals() method is invoked which prints the hashcode of the object .

Instead try using System.out.println(Arrays.toString(dataArray));

Comments

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.