Search code examples
javaint

get second digit from int


I have an int that ranges from 0-99. I need to get two separate ints, each containing one of the digits. I can't figure out how to get the second digit. (from 64 how to get the 6) This is my code:

public int getNumber(int pos, boolean index){//if index = 1 - first digit, if index = 0 - second digit 
    int n;
    if(index){
        n = pos%10;
    }else{
        if(pos<10){
            n=0;
        }else{
            //????
        }
    }
    return n; 
}

Solution

  • You can do integer division by 10. For example, in the following code res should equal 4:

    int res = 42 / 10;