-1

I have a string of "A B C" and I have this line of code to split it:

String[] reFormatted = TestString.split("   ");
System.out.println(reFormatted.toString());

and the output I get is this:

[Ljava.lang.String;@30f1c0

if I put this string array in a foreach loop like this:

for(String s : reFormatted){
   System.out.println(reFormatted);
}

I get the output of:

[Ljava.lang.String;@30f1c0
[Ljava.lang.String;@30f1c0
[Ljava.lang.String;@30f1c0

what is the problem here? what am I doing wrong?

Anthony
  • 69
  • 9
  • 1
    Try `for(String s : reFormatted){ System.out.println(s); }` – The fourth bird Feb 08 '20 at 09:10
  • reFormatted is an array type, you can check it by printing this `reFormatted.getClass().getName()` to check the type of the variable. And you cannot print the array, it will return you the address of where the array is stored in the memory. Instead, you need to print the elements of the array, as others pointed out, print the variable `s` in the for loop. Good Luck! – Aashish Chaubey Feb 08 '20 at 09:24
  • To print an array you can write ```System.out.println(Arrays.asList(reFormatted))``` – Stefan Feb 08 '20 at 09:35

1 Answers1

1

I think you are printing the address of the variable reFormatted and for the for loop, you should print s not reFormatted

for(String s : reFormatted){
    // System.out.println(reFormatted);
    System.out.println(s);
paul-shuvo
  • 1,874
  • 4
  • 33
  • 37