Results 1 to 11 of 11

Thread: Help with Loops

  1. #1
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Help with Loops

    im trying to write some code for an input validation

    basically im asking the user to input a 1,2, or 3

    how do i validate their input and display an error when they put something other than the 3 choices using something like a while loop.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    use if statements.

  3. #3
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    if (iBuy < 1 || iBuy > 3)

    works if the user enters an integer <1 or >3 but what if its not an integer?

  4. #4
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Make sure to do <= and >=, because right now the above code is only going to include the input 2, because thats the only integer between 1 and 3 NOT INCLUDING 1 and 3.

    Your going to need to basically write that user must have inputs 1,2, or 3. You can use "else if" then print a line telling the user "invalid input, please try again".

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    how about..
    Java Code:
    if(in <= 3 || in >= 1){
      //do something with input
    }
    else{
      //report error
    }
    You could also have a method which tests if input is a number or not, first apply that, if its true test if its in range

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by </3java View Post
    Make sure to do <= and >=, because right now the above code is only going to include the input 2, because thats the only integer between 1 and 3 NOT INCLUDING 1 and 3.

    Your going to need to basically write that user must have inputs 1,2, or 3. You can use "else if" then print a line telling the user "invalid input, please try again".
    That is pretty much correct, except he used < and > because he was checking if the number was outside of range. ibuy < 1 would be true if ibuy is 0, but not 1.

  7. #7
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    System.out.println("Would you like to:");
    			System.out.println("1. Buy");
    			System.out.println("2. Sell");
    			System.out.println("3. End your trading");
    			int userChoice = in.nextInt();
    
    				if (userChoice < 1 || userChoice > 3)
    				{
    					System.err.println("Invalid input: enter 1, 2, or 3.");
    					System.out.println("Would you like to:");
    					System.out.println("1. Buy");
    					System.out.println("2. Sell");
    					System.out.println("3. End your trading");
    					userChoice = in.nextInt();
    			
    				}
    
    			//if user chose buy
    				else if (userChoice == 1);
    //etc...
    how can i make so that everytime the user doesnt enter 1,2, or 3 it does that loop. also i still dont have the if statement right. if i enter a character (eg. a) it will crash.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Two things I would like to tell you, the first, you can use a switch statement to make the code a bit easier
    Java Code:
    switch(num){
      case 1:  //do something
      case 2: // do something
      case 3:  //do something
    }
    Look up switch statements and read about it before you use them.

    You can do this whole problem very easy with an else clause
    Java Code:
    if(input == 1){
      //do something
    } 
    ....
    else{
      //invalid input stuff
    }

  9. #9
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    while (!userThrow.equalsIgnoreCase("rock") &&
                     !userThrow.equalsIgnoreCase("scissors") &&
                     !userThrow.equalsIgnoreCase("paper"))
                {
                    System.out.println("Invalid throw: " + userThrow);
                    System.out.println("Enter rock, paper, or scissors:");
                    userThrow = in.nextLine();
                }
    i found this for a rock paper scissors program. i was wondering if there is anyway to use this type of loop to check for 1,2, or 3.

  10. #10
    javaforum$ is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default Try while with switch

    public static void main(String[]args)
    {
    printUsage();
    Scanner in = new Scanner(System.in);
    while (in.hasNext())
    {
    try
    {
    switch (in.nextInt())
    {
    case 1 :
    {
    System.out.println("Have a Nice Buy");
    System.out.println("############################## ###########");
    System.out.println("Would you like to continue y/n ?");
    break;
    }
    case 2 :
    {
    System.out.println("GoodLuck with your Sell");
    System.out.println("############################## ###########");
    System.out.println("Would you like to continue y/n ?");
    break;
    }
    case 3 :
    {
    System.out.println("Thanks for trading, Visit Again");
    System.out.println("############################## ###########");
    return ;
    }
    default :
    {
    printUsage();
    continue;
    }
    }
    }
    catch (Exception e)
    {
    System.out.println("---------------------------------------");
    System.err.println("You have choosen a wrong option,");
    printUsage();
    }

    String contn = in.next();
    if ("n".equalsIgnoreCase(contn))
    {
    break;
    }
    else if ("y".equalsIgnoreCase(contn))
    {
    printUsage();
    continue;
    }

    }

    System.out.println("##############The End###########################");
    }

    public static void printUsage()
    {
    System.out.println("****************************** *******");
    System.out.println("Please make the below any one choice:");
    System.out.println("1. Buy");
    System.out.println("2. Sell");
    System.out.println("3. End your trading");
    System.out.println("****************************** *******");

    }

  11. #11
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default problem with loop

    when user enters invalid value which is not int in generates NumberFormatException you can catch it and in catch block you can set the value of errorstatus kind of variable...

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 01:36 PM
  2. loops
    By curioustoknow in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 01:45 PM
  3. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 06:51 PM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  5. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •