Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print/log array of string in java?

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.

like image 599
Rachel Avatar asked Jun 25 '12 19:06

Rachel


People also ask

How do I print a string array in logs?

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.

How do you write a string array in Java?

You can also initialize the String Array as follows:String[] strArray = new String[3]; strArray[0] = “one”; strArray[1] = “two”; strArray[2] = “three”; Here the String Array is declared first. Then in the next line, the individual elements are assigned values.

How do you print a string array without loop?

Pseudo Code:println(Array[i]); Concept: We will be using the toString() method of the Arrays class in the util package of Java. This method helps us to get the String representation of the array. This string can be easily printed with the help of the print() or println() method.


2 Answers

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

like image 137
Matt Ball Avatar answered Sep 22 '22 20:09

Matt Ball


use Arrays.toString(array); it will work

like image 42
dantuch Avatar answered Sep 26 '22 20:09

dantuch