3

And i have no idea why!

I bascially have a STRING (yes, not an array), that has the following contents:

[something, something else, somoething, trallala, something]

And i want to turn it into a String[]. So first off i substring() off the first and the last character to get rid of the brackets []. Then i use the split() function to split by comma. I tried using both "\|" and "," and "\," with the same results.

This is what i get:

[Ljava.lang.String;@186d4c1

Here's the code for it. I made it into a one-liner:

String[] urlArr = ((matcher.group(3).toString()).substring(1, (matcher.group(3).length()-1))).split(",");

As you can see the first part is (matcher.group(3).toString()), and it DOES return a valid string (like the example i posted above). So i don't get why it's not working.

Any ideas?

EDIT:

I clarified the code a bit:

String arrString = matcher.group(3).toString();
int length = arrString.length();
String[] urlArr = (arrString.substring(1, length-1)).split(",");
System.out.println(urlArr);
qwerty
  • 383
  • 3
  • 7
  • 14

3 Answers3

8

The output

[Ljava.lang.String;@186d4c1

is the way java prints a string array (or any array) by default (when converted to a string). You may use

Arrays.toString(urlArr)

to get a more readable version.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • Alright, but how would i go about to loop through all the items? Like a foreach loop? How do i get the actual value instead of that code? Thanks! – qwerty Sep 22 '11 at 16:00
  • I think this is a better solution than converting it to a list to use the List's toString(), like Peter suggested. Shouldn't matter too much though since it's probably for debug only. – Jules Sep 22 '11 at 16:00
  • What do you mean by "value"? You get an array of strings which you can loop over (e.g. as you suggested by a for loop: `for (String s : urlArr) { System.out.println(s); }`). – Howard Sep 22 '11 at 16:01
  • No, it's not for debug only. The string i'm parsing is from a config file, and i need to split the values into a new array to load into the program. – qwerty Sep 22 '11 at 16:02
  • But i can't loop through a string right? If i use the Arrays.toString(), it returns a string, and i can't loop through a string? See where i'm going? I might not be getting you right :/ – qwerty Sep 22 '11 at 16:03
  • If you're interested look here for an [explanation](http://stackoverflow.com/questions/3442090/java-what-is-this-ljava-lang-object) of the output. – Dario Seidl Sep 22 '11 at 16:06
5

You are getting a valid array of Strings, but trying to print it directly does not do what you would expect it to do. Try e.g.

System.out.println(Arrays.asList(urlArr));

Update

If you want to process the parsed tokens one by one, you can simply iterate through the array, e.g.

for (String url : urlArr) {
  // Do something with the URL, e.g. print it separately
  System.out.println("Found URL " + url);
}
Péter Török
  • 114,404
  • 31
  • 268
  • 329
2

When you do toString on an Array, you just get the internal representation. Not that useful. Try Arrays.toString(what comes back from split) for something more readable.

Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53