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

Thread: How to use a while loop to perform input validation?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use a while loop to perform input validation?

    so let''s say I have a rock paper scissors program and I want the user to enter ONLY rock, paper and scissors. If the user enters anything else, the program would display an error message and ask the user to try again. How would I do this in java?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to use a while loop to perform input validation?

    Show us some code that does any of what you describe, and we'll help you add the features you need.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to use a while loop to perform input validation?

    This is quite straight forward name (don't mind if I call you name do you?). It can be split up into 3 main components. So where to begin? Well we need to define what characters you define to be legal. We would do this as follows:

    final String[] legal = {"rock", "paper", "scissors"};

    Now we have a clean and simple data structure storing all the values you are willing to accept. But how does this translate into checking if a value matches? Well easy... with an array, we dont need to fiddle about with a long column of ifs and elses, we just need a simple loop.

    	private static boolean isLegal(String input, String[] legal){
    		for(String value : legal){
    			if(value.equals(input.toLowerCase())){
    				return true;
    			}
    		}
    		return false;
    	}

    If the input string matches any of the array content, the loop will stop in its tracks and return TRUE. This indicates that a match has been found. However, if the input doesn't match any of the legal keywords, it will exit the loop and return FALSE as default. This method means we can easily and cleanly check for a value inside the loop, and anytime you want to add an extra keyword, all you need to do is add it to the original array.

    So we've got the legal keywords, we know how to check if a word matches the keywords, all we need now is the main loop. We start off by using a Scanner object. Using the scanner, we poll the user for an input and once we receive it, we check the loop condition before we re-enter or exit the loop.

    		final Scanner scanner = new Scanner(System.in);
    		String input = null;
    		do{
    			input = scanner.nextLine();
    		}while(!isLegal(input, legal));

    Using a do-while loop, we can immediately enter the loop without checking anything, but in-order to exit the loop and not be asked to input another value, the condition must be satisfied. Using the method we made, we can check if the contents of the array match the users input. If its not legal, we re-enter the loop.

    Good luck.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use a while loop to perform input validation?

    import java.util.*;

    class rps{
    public static void main(String args[]){
    Random number = new Random();
    Scanner input = new Scanner(System.in);

    // player chooses
    String choice;

    System.out.println("Enter your throw (rock, paper, scissors)(enter exactly as shown): ");

    choice = input.nextLine();

    //input validation loop
    while(choice!="rock" && choice!="paper" && choice!="scissors"){
    System.out.println("error, try again");
    System.out.println("Enter your throw (rock, paper, scissors)(enter exactly as shown): ");
    break;
    }

    System.out.println("The player throws "+choice);

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to use a while loop to perform input validation?

    Use String.equals(String) to compare Strings. For objects (which a String is), .equals() method compares "equality", while == compares if they are physically the same object in memory.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use a while loop to perform input validation?

    can you show me exactly how to do it?

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to use a while loop to perform input validation?

    Did I just give you the code and a full explanation for nothing? >.>
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #8
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use a while loop to perform input validation?

    Quote Originally Posted by newbie View Post
    Did I just give you the code and a full explanation for nothing? >.>
    I just want a SIMPLE input validation loop

  9. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to use a while loop to perform input validation?

    Your sort really drive me up the wrong way. Pull your finger out of your arse for once and read.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #10
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use a while loop to perform input validation?

    Quote Originally Posted by newbie View Post
    Your sort really drive me up the wrong way. Pull your finger out of your arse for once and read.
    it's spelled "ass" not "arse". Get your spelling correct.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to use a while loop to perform input validation?

    That spelling reflects the North Wales pronunciation along with some of the more rural parts of the US.

    But let's reset to your problem. Simple input validation is:

    // wordy pseudo-code
    get input
    if input does not match the required values:
    - post error message and return to "get input"
    otherwise, continue with processing input

    What help do you need to program that?

    Note that the if statement could be changed to the positive:

    if input DOES match the required values, etc.

    That approach may simplify the looping.

  12. #12
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to use a while loop to perform input validation?

    Funnily enough, American English is different to British English.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Replies: 1
    Last Post: August 9th, 2013, 10:26 AM
  2. Replies: 1
    Last Post: May 29th, 2013, 06:22 AM
  3. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  4. input controlled loop
    By pezbirdy in forum Loops & Control Statements
    Replies: 4
    Last Post: November 10th, 2011, 06:16 AM
  5. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM