Hi,

I am making a program where the user has to input an integer with LESS than 5 digits.

If he enters more than 5 digits, I want the program to print: "ERROR"

How can I do that?

I wrote this code till now. Do I have to use a for loop? If yes what should I write in it?:)

Scanner input = new Scanner(System.in);
        int number = 0;        
        System.out.print("Enter a number with less than five digits:");
        number = scan.nextInt();
        for(int counter=0;counter<=5;counter++){
            

        }

Dani AI

Generated

You do not need a for-loop to count digits. Treat this as input validation: read a token, check that it is an int, then check that its absolute value is less than 10000 (ie, 1-4 digits ignoring the sign). This avoids using String.length, which said has not been covered yet, and it also handles non-numeric input cleanly. and are right that checking the representation as a String works too, but a numeric bound is simpler for where you are. Also, the largest 5-digit number is 99999 and the smallest 6-digit number is 100000 (not 1000000).

Example workflow that keeps prompting until the input is valid:

Scanner sc = new Scanner(System.in);
int n;

while (true) {
    System.out.print("Enter an integer with fewer than 5 digits: ");
    if (!sc.hasNextInt()) {
        System.out.println("ERROR");     // not an integer
        sc.next();                       // discard invalid token
        continue;
    }
    n = sc.nextInt();
    if (Math.abs(n) >= 10000) {
        System.out.println("ERROR");     // 5+ digits
        continue;
    }
    break; // OK: n has 1-4 digits (sign ignored)
}
// use n

Notes and edge cases:

  • Negative numbers: -1234 is valid because it has 4 digits; the minus sign does not count.
  • Leading zeros: if the user types 00012, reading as an int turns it into 12 (2 digits). If you must count the typed characters instead, read a line and validate it against a pattern like ^-?\\d{1,4}$, then parse it.
  • Be consistent with your Scanner variable name; if you declare Scanner input, call input.nextInt(), not scan.nextInt().

Recommended Answers

All 7 Replies

Just use if else statement.

EDIT:

Sorry, my bad. I thought you want an input that is less than 5(1 digit).

Here is my suggestion:

1. Take the input as string.
2. Check the length of it.
3. If it is less than 5, convert it into integer, and then proceed with whatever you wanna do with it, else just print "Error".

Just read the number as a String, give an error if the length of the String is too long.
Also give an error if the input can't be interpreted as an integer.

The standard library has methods to test both conditions.

We cannot use the length because we didn't take it yet. Is there another way?:)

We cannot use the length because we didn't take it yet. Is there another way?:)

What do you mean by "we didn't take it yet"?

We check the length only after the user input the number.

What do you mean by "we didn't take it yet"?

We check the length only after the user input the number.

I am understanding what you are saying but what I meant was that we still didn't take how to check the length in COLLEGE yet.

Scanner input = new Scanner();

yourInput = input.nextInt();

String temp = Integer.toString(yourInput);

if(temp.length()<5) {
//Do what you want
}

else {
System.out.println("ERROR");
}

And in case you want to have the same input as an int after the check, insert this after the if:

yourInput = Integer.parseInt(temp);

If you really really can't use length, remember that the largest 5 digit positive integer is 99999, and the smallest number that's 6 or more digits is 1000000, so you can just test the numeric value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.