0
package bankaccountapp;
import java.io.*;
import java.util.*;
public class Csv {
public static void main(String args[]){
    List<String[]> accounts=new LinkedList<String[]>();
    String data;
    try {
        BufferedReader br = new BufferedReader(
            new FileReader(
            "C:\\Users\\RaviKiran Reddy\\Desktop\\JBNR\\NewBankAccounts.csv"));
        while ((data=br.readLine())!= null) {
            String[] datarows=data.split(",");
            accounts.add(datarows);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e1) {
        e1.printStackTrace();
    }

    System.out.println(accounts);
}

When I try to read a CSV file by splitting it using commas I am getting the object code (like [Ljava.lang.String;@7382f612) as output, but not the strings as I expected.

If this is the line: Deadra Power,009545701,Checking,4500

then I am expecting the output as: \n Deadra Power \n 009545701 \n Checking \n 4500.

Can I know where the error is?

1
  • 2
    As it appears that you got an useful answer you should accept (and maybe upvote) one of them in order to help other people finding correct information. Commented Sep 13, 2018 at 16:04

4 Answers 4

1

You are inserting the whole string array into accounts so I guess that [Ljava.lang.String;@7382f612 is the result of the toString() method of the array.

Try accounts.get(0)[0] for the first field of the first row, accounts.get(0)[1] for the second field of the first row, etc.

If you want to obtain data as \n Deadra Power \n 009545701 \n Checking \n 4500 then don't split each read line, instead replace commas by \n.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah,i got it .
1

You are calling System.out.println() on a List.

This calls the System.out.println(Object), which won't know how to recurse into your object.

Have a look at:

How do I print my Java object without getting "SomeType@2f92e0f4"?

You need to get each element out manually and print it.

1 Comment

yeah i got it .
1

As Paco Abato pointed out, you are printing the object that is an array of strings.

If you'd like to see each element of the array (i.e. a row from the csv file), you can print it as follows:

for (String[] accountRow : accounts) {
   System.out.println(Arrays.toString(accountRow));
}

Comments

1

Making

 System.out.println(accounts);

Will call the toString() method and it will print a string representation of the object. For that you need a simple function to read Array of strings and the print each string of you List. So with this you will have the access of each element of you List. Hope it Help you.

for (String[] myLine : accounts)
{
    // To do the magic
}

1 Comment

yeah i got it now

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.