## subone Newbie Poster

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++){

}``````

## leodash

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".

## jwenting 1,649

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.

## subone

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

## leodash

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.

## subone

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.

## geekman89

``````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);``

## JamesCherrill 4,219

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.