30

Little Background:

I have csv file which has lots of rows and each row has string elements, one example of such a row would be

String[] data = [20,11,Clothing,TShirts,Abercombie,Gap]

data.toString() = [Ljava.lang.String;@1152e94]

Now in my parser, I am parsing this csv file and getting each row present in the file as String[] data. In my log page, I need to have the id as well as the row present in the file.

Currently, if I try to print then am getting values like [Ljava.lang.String;@1152e94, my question is how can i get actual list of array elements like [20, 11, Clothing, TShirts, Abercombie, Gap]?

Tried using default toString() but still it give same LString data.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Rachel
  • 100,387
  • 116
  • 269
  • 365

4 Answers4

66

Use Arrays.toString(array) to obtain something more human-readable.

uı6ʎɹnɯ ꞁəıuɐp
  • 3,431
  • 3
  • 40
  • 49
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
11

use Arrays.toString(array); it will work

dantuch
  • 9,123
  • 6
  • 45
  • 68
1

May be: What's the simplest way to print a Java array? ?

String[] data = [20,11,Clothing,TShirts,Abercombie,Gap]
Arrays.toString(data);

or

Arrays.deepToString(data);
Community
  • 1
  • 1
Anton Bessonov
  • 9,208
  • 3
  • 35
  • 38
0

Read a line of the csv file and call String.split(",") on the line which will return you an array of each seperate string in the line you just read then you can simply loop through the array and print them out as you need.

String csvLine = scanner.nextLine();

String[] values = csvLine.split(",");
for(String s : values){
  System.out.println(s);
}
ChadNC
  • 2,528
  • 4
  • 25
  • 39