import java.util.Scanner;
public class asg3 {
public static int setoriginal() {
int d=0;
int l=0;
do {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a 4 digit number: ");
d=input.nextInt();
if (d<=9999 || d>999) {
break;
} else {
System.err.printf ("You did not enter 4 digits\n");
}
l=l+1;
} while (l<3);
return d;
}
public static void main(String[] args) {
setoriginal();
}
}
when I enter 12345 it works but doesn’t seem to work for any other number other than four-digit ones.
any idea of what could be wrong or what I am doing wrong?
Look at this condition:
For that to evaluate to false, you would have to find a value which is greater than 9999 and less than 1000. I think you’ll be hard pushed to find such a number…
I suspect you meant:
Personally I’d write this as:
or
In all these versions, you’re basically checking that it’s greater than or equal to 1000 and it’s less than or equal to 9999.