0

I have tried inputing the same int as the validPin variable, but it makes infinite loop even though the condition is already true. Also, I have tried to change validPin to 12345 and it worked, but if the validPin is 002200 it doesnt work. Why?

public void main() {

    Scanner in = new Scanner(System.in);
    int validPin = 002200;
    System.out.print("Enter PIN to access:");
    int pin = in.nextInt();


    while (pin != validPin) {
        System.out.print("Invalid PIN, Try again:");
        pin = in.nextInt();
    }

    System.out.println("You have entered the correct PIN, now you have acces for your account!");

}

}

mrdoubleu
  • 31
  • 6
  • 2
    `002200` is `2200` for an int, did you try it? Maybe you should use strings instead primitive integers – SerCrAsH Apr 14 '21 at 22:53

1 Answers1

3

In Java source code, 002200 is the decimal value 1152, and for nextInt, 002200 is the decimal value 2200.

(This is an annoying throwback from C that should never have been adopted in Java, but we're stuck with it).

Read about octal constants in Java source code - any tutorial should have this information.

Also, if you require the user to actually type those leading zeroes, you may be better off using strings rather than ints.

user15187356
  • 807
  • 3
  • 3