-1

I try to convert a CharacterSequence directly into a integer array with the single integers.

CharSequence nbr = "478";
int j;
int[] testArray = new int[100];

for(j = 0; j <= nbr.length() - 1; j++)
    testArray[j] = Character.getNumericValue(nbr.charAt(j));

System.out.println(testArray);

Instead of the desired [4,7,8] the Console gives back something like that:

[I@424c2849

Setting up a switch with the cases '0','1',...,'8','9' and corresponding assignment did not solve the problem.

I hope you can help me! Thanks in advance ;)

jns
  • 1,250
  • 1
  • 13
  • 29

2 Answers2

1

Arrays don't override the toString method. So, when you try to print any array, the Object class toString method is invoked, and you get the representation returned by that method, which is of the form - Type@hashCode

To get the required representation, use Arrays.toString method to print array: -

System.out.println(Arrays.toString(testArray));

Apart from that, you really should declare your integer array as:

int[] testArray = new int[nbr.length()];

rather than using 100 size.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

Try with

    CharSequence nbr = "478";
    int j;
    int[] testArray = new int[100];

    for(j = 0; j <= nbr.length() - 1; j++)
        testArray[j] = Character.getNumericValue(nbr.charAt(j));

    for(int i : testArray){
        System.out.println(i);
    }
matteo rulli
  • 1,443
  • 2
  • 18
  • 30
  • This will work (sort of ... you'd get multiple lines each with one number), but you really should be using the `Arrays.toString()` method provided. – Brian Roach Feb 09 '13 at 22:47