I wrote a method to read in a txt file and then return the value(s) line by line. However, the value(s) are being returned as such - [Ljava.lang.String;@6f579a30. I would have expected something different. The .txt file I'm reading in has value(s) in the file like this...

A
B
C
D

Any help would be greatly appriciated!

public String[] readFile()
	{
		//Go fetch the file that I want to read
		File file = new File("path-file.txt"); 
	
			//String[] readLines(file); {         
			FileReader fileReader = null;
			try {
				fileReader = new FileReader(file);
			} catch (FileNotFoundException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}         
			BufferedReader bufferedReader = new BufferedReader(fileReader);        
			
			List<String> lines = new ArrayList<String>();         
				String line = null;         
			try {
				while ((line = bufferedReader.readLine()) != null) {             
					lines.add(line);         }
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}         
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
				String[] answer = lines.toArray(new String[lines.size()]);  
				System.out.println(answer);
			return answer;    
			}
Member Avatar for hfx642

You're trying to print an array, rather than each line in the array.
try...

for (int I = 0; I < answer.length; I++)
   System.out.println (answer [I]);

the value(s) are being returned as such - [Ljava.lang.String;@6f579a30

Where do you see the BOLD part shown above?
It looks like you are printing the array. I think you want the contents of the array.
Try this:

String[] anArray = new String[] {"asdf", "awer"}; // define 2 dim array
    System.out.println("anArray=" + anArray);   // anArray=[Ljava.lang.String;@3e25a5

To print the contents see the bit of code in the previous post.
Another way to print contents:

String[] anArray = new String[] {"asdf", "awer"};  // define 2 dim array
    System.out.println("anArray=" + Arrays.toString(anArray));   // anArray=[asdf, awer]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.