# User entry needs to be exactly 5 digits

• September 29th, 2012, 10:31 PM
tyneframe
User entry needs to be exactly 5 digits
Just starting out in Java and need a little help.
I'm writing a simple code where the user is asked to enter a 5 digit number, otherwise they get a 'please try again' message. I've figured out most of it, except the number 00000 (with five zeros) must also be an acceptable entry, but not acceptable if they were to enter just one zero. Here's what my if statement looks like so far:
Code java:

```if(userEntry < 100000 && userEntry > 9999){ System.out.println("Your chosen number is: "+userEntry); }else{ System.out.println("Your entry is invalid. Please try again."); main(args); }```
How do I write the code to make 00000 an acceptable entry, but not 0?
• September 29th, 2012, 10:35 PM
curmudgeon
Re: User entry needs to be exactly 5 digits
Quote:

Originally Posted by tyneframe
Just starting out in Java and need a little help.
I'm writing a simple code where the user is asked to enter a 5 digit number, otherwise they get a 'please try again' message. I've figured out most of it, except the number 00000 (with five zeros) must also be an acceptable entry, but not acceptable if they were to enter just one zero. Here's what my if statement looks like so far:

Code :

```if(userEntry < 100000 && userEntry > 9999){ System.out.println("Your chosen number is: "+userEntry); }else{ System.out.println("Your entry is invalid. Please try again."); main(args); }```

How do I write the code to make 00000 an acceptable entry, but not 0?

You could read it in as a String and check that the String length == 5. Then parse it into an int and make sure it parses OK by doing it inside of a try/catch block.

As an aside, you're repeating your main method by using recursion -- by having the main method call itself, and I'm not sure that I'd do things this way. Why not instead use a simple while loop?
• September 29th, 2012, 10:45 PM
tyneframe
Re: User entry needs to be exactly 5 digits
Repeating the main method seemed like the simplest thing to re-run the code and allow the user to re-enter a number, if their first try was invalid. I am new to this...only a couple of weeks into the course and don't really know anything about loops yet.

I'll gladly give it a try. Do you know where I could find a tutorial or explanation of the code?

And thank you for the quick help.
• September 29th, 2012, 10:52 PM
curmudgeon
Re: User entry needs to be exactly 5 digits
Quote:

Originally Posted by tyneframe
Repeating the main method seemed like the simplest thing to re-run the code and allow the user to re-enter a number, if their first try was invalid. I am new to this...only a couple of weeks into the course and don't really know anything about loops yet.

I'll gladly give it a try. Do you know where I could find a tutorial or explanation of the code?

And thank you for the quick help.

You're welcome.

As for a tutorial, the best are to be found here:
• September 29th, 2012, 10:54 PM
tyneframe
Re: User entry needs to be exactly 5 digits
Thank you very much for the help and links. I'll check them out.