0

So I wrote a method which takes a string and converts each letter into an int via for loop. Each time the loop executes, I made it print out each value and add the value to a int array. The letter values should be from 0 to 25 which is why I have the c - 97. The print statements print the right number for each letter however at the end I also have it print out the entire array and I get a weird thing that it's printing. I don't think I'm adding the value correctly to the array so what am I doing wrong?

public int[] stringToIntArray(String text){
    int ascii;
    char c;
    int a;
    int[] array = new int[text.length()];
    for (int i=0; i<text.length(); i++){
        c = text.charAt(i);
        a = c - 97;
        array[i] = (a);
            if (i != array.length - 1){
                System.out.print(a + ", ");
            } else {
                System.out.print(a + " ");
            }

    } 
    System.out.print(array);
    return array;
}
David Rolfe
  • 163
  • 2
  • 10
  • 2
    [Avoid magic numbers](http://stackoverflow.com/q/47882/1393766). Instead of `97` use `'a'`, it will be converted to 97 for you but your code will be clearer. – Pshemo Jan 28 '15 at 21:20

1 Answers1

1

You need to use Arrays.toString(). You're currently printing a memory address.

System.out.println(Arrays.toString(array));
haley
  • 1,165
  • 7
  • 13