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?

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
ravikiran
  • 33
  • 6
  • 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. – Paco Abato Sep 13 '18 at 16:04

4 Answers4

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.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
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.

user1717259
  • 2,717
  • 6
  • 30
  • 44
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));
}
hinson
  • 151
  • 3
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
}
Gatusko
  • 2,503
  • 1
  • 17
  • 25