-1

I'm having a problem when I try to parse a String to a char array. Here is my code:

line = scan.nextLine();
System.out.println(line);               
char line2[] = line.toCharArray();
System.out.println(line2.toString());

So, as you can see, it's a simple code.

The problem is: line contains the string "00000001010010110100100000100000", but when I use "line.toCharArray", my char array receives "[C@7e243eed". I think it's receiving the line variable adress or something like that.

Could someone help me? Thanks :D

David Moles
  • 48,006
  • 27
  • 136
  • 235
Ramon Machado
  • 15
  • 1
  • 6
  • 4
    `System.out.println(Arrays.toString(line2));` – Ousmane D. Jun 16 '17 at 22:32
  • 1
    You could also use `System.out.println(line2)` (without explicit `toString()` call). This would invoke overloaded [`println(char[])`](https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println-char:A-) version of `println` which prints each character (without `[ , , , ]` array format). – Pshemo Jun 16 '17 at 22:42
  • Thank you! I didn't know that I could do that :D – Ramon Machado Jun 16 '17 at 22:46

1 Answers1

3

String to char array conversion is fine, it's Sysout that prints hash of the array object instead of its content. If you want to print the array in user friendly way, use:

System.out.println(Arrays.toString(line2));
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Thanks, mate! I didn't know that. In my code, there's another problem too... I was trying to create a new string using "String s = line2.toString()" (as I was doing inside the print), but instead I should use "String s = new String(line2)". Thank you! – Ramon Machado Jun 16 '17 at 22:41