Jerry Murphy
Jerry Murphy

Reputation: 348

I have created an Int array from a Char array now the values don't match

Ok so when I use this code instead of a 9999 I get 57 for each 9? Pretty confused new to Java any help would be appreciated!!

public int[] getNumber(){

    String newNum;

    Scanner input = new Scanner( System.in );

    System.out.println( "Please enter four digits" );
    newNum = input.nextLine();
    char[] list = newNum.toCharArray();
    int[] numArray = new int[newNum.length()];

    for (int i = 0; i < newNum.length(); i++){
        numArray[i] = list[i];
    }

    return numArray;
}   

Upvotes: 0

Views: 43

Answers (1)

JB Nizet
JB Nizet

Reputation: 691725

Yes. 57 is the integer value of the char '9'. So it's expected. If you want the numeric value of the char, use

numArray[i] = list[i] - '0';

Upvotes: 1

Related Questions