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 7 of 7

Thread: Java integer limit

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java integer limit

    Hi, does anyone know how to set a limit to a number a user inputs? I am making a practice program that determines the price of a customers order and I need to set a limit to how many items they are allowed to purchase. The limit is 40.

    here is what I have:

    if ( number >40 )
    System.out.print( "Error: Limited number of items available. Please enter a number under 40. " );

    This works for displaying an error message, however I would like the customer to be able to enter another number after they receive that error. So it would look like this

    Please enter a number: 45
    Error: Limited number of items available. Please enter a number under 40.
    Please enter a number: <---------(loops back to original question)

    Thanks!!!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java integer limit

    There is a "one and a half" loop design pattern:

    Ask user for input
    while(input not valid):
        print error message
        Ask user for input

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java integer limit

    I'm not quite sure I understand what you mean. Can you elaborate? I'm very new to programming.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java integer limit

    The while loop following the user inputting some data won't execute if the data in valid.
    If the data is not valid, the while loop will execute and continue looping until the user enters good data.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java integer limit

    So I should use a while loop??

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java integer limit

    Try coding it like shown in post#2
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java integer limit

    A while loop would make sense (I used it in my pseudo-example).

Similar Threads

  1. Java algorithm to print all the combination of the integer array
    By ashish12169 in forum Algorithms & Recursion
    Replies: 11
    Last Post: May 2nd, 2013, 05:55 PM
  2. Synchronized a integer variable in Java
    By hellokitty25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 24th, 2013, 09:49 PM
  3. JTextField character limit
    By hump_truck in forum AWT / Java Swing
    Replies: 1
    Last Post: May 31st, 2012, 07:17 AM
  4. HELP: I have problem casting from Vector to Integer in Java
    By tintin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2011, 12:39 PM
  5. How to make Java determine if a values is an integer or not.
    By MiniatureBeast in forum Object Oriented Programming
    Replies: 5
    Last Post: August 17th, 2011, 11:32 PM