Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: User entry needs to be exactly 5 digits

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:
    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?
    Last edited by helloworld922; September 29th, 2012 at 09:51 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: User entry needs to be exactly 5 digits

    Quote Originally Posted by tyneframe View Post
    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:

    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?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: User entry needs to be exactly 5 digits

    Quote Originally Posted by tyneframe View Post
    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:

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User entry needs to be exactly 5 digits

    Thank you very much for the help and links. I'll check them out.

Similar Threads

  1. How to sort a Map.Entry in a for loop?
    By Porknbeans in forum Collections and Generics
    Replies: 1
    Last Post: March 23rd, 2012, 07:02 AM
  2. Table with CustomModel when clicked shows old entry
    By Nesh108 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 9th, 2011, 06:38 AM
  3. All Digits only
    By kram in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2011, 03:16 PM
  4. need help w/ entry level homework...
    By rbread80 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 3rd, 2010, 10:24 PM
  5. how to add a new entry in a current array
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 28th, 2009, 06:39 PM