Home > Net >  Why does hard-coding in numbers work, but using variables with the same values, doesn't?
Why does hard-coding in numbers work, but using variables with the same values, doesn't?

Time:02-14

I'm writing a program that is a game where you have to guess whichever random 4 digit integer that the computer generates. I'm having trouble with the input validation - more specifically, my isDigit() method, which aims to make sure all inputs from userGuess are digits 0 - 9:

    private static boolean isDigit(char indexPos) { // verifies if the input char is a digit 0-9 or not
        if(indexPos == 0 || indexPos == 1 || indexPos == 2 || indexPos == 3 || indexPos == 4 || indexPos == 5 || indexPos == 6 || indexPos == 7 || indexPos == 8 || indexPos == 9) {
            return true;
        }
        else {
            return false;
        }
    }

If I hardcode in things like

isDigit(1)

it evaluates as true, but if I plug in variables, it returns false. Mainly, this returns false, even when all index positions of userGuess are digits 0-9:

isDigit(userGuess.charAt(i)) // returns false even when index pos i is [0-9]

I'm utterly confused and stumped as to why this happens. For context, here's the whole if and for loop situation for the validation:

numGuess  ;
System.out.print("Guess "   numGuess   ": ");
userGuess = kbd.nextLine();
System.out.println("DEBUG: userGuess.length() : "   userGuess.length());

// validate that the input is a valid input (a 4 digit natural number, incl 0000)
if(userGuess.length() < 4 || userGuess.length() >= 5){
   System.out.println("Please enter a valid input, which is a 4 digit natural number (postive integer, including 0000).");
}
else if(userGuess.length() == 4) {
   for(int i = 0;  i <= 3; i  ) {
     if(isDigit(userGuess.charAt(i)) == true) { 
        validateCount  ;
     }
}

I also have, what I assume to be the same issue, on validating if userGuess.length() == 4. If I output userGuess.legnth() on the console, it provides the correct number, and when == 4, this if loop doesn't run, even though the condition is met:

if (userGuess.length() < 4 || userGuess.length() >= 5) {
   System.out.println("Please enter a valid input, which is a 4 digit natural number (postive integer, including 0000).");
} else if (userGuess.length() == 4) { // avoid out of bounds errors for inputs less than 4 digits
   for (int i = 0; i <= 3; i  ) { // userGuess will never have an index above 3, as it is a 4 digit number
if (isDigit(userGuess.charAt(i)) == true && userGuess.length() == 4) { // verify that the userInput is a 4 digit integer
    validateCount  ;
}

CodePudding user response:

You are comparing the char indexPos with the integers from 0 to 9. Try comparing indexPos with '0','1','2',... That way you compared if the char indexPos with another char.

CodePudding user response:

If you are really working with characters, then you could do this.

private static boolean isDigit(char indexPos) { 
    return Character.isDigit(indexPos);
}

But then, you don't really need a new method. In any event I would suggest you work with actual ints as the argument name indexPos suggests.

CodePudding user response:

Just pass an integer instead of a char and it will work.

private static boolean isDigit(int indexPos) {
    if(indexPos == 0 || indexPos == 1 || indexPos == 2 || indexPos == 3 || indexPos == 4 || indexPos == 5 || indexPos == 6 || indexPos == 7 || indexPos == 8 || indexPos == 9) {
        return true;
    }
    else {
        return false;
    }
}

You also can write the method a lot shorter:

private static boolean isDigit(int indexPos) {
   return indexPos>=0 && indexPos<=9;
}
  • Related