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

Thread: need help looping with a string

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help looping with a string

    okay so i have the user being prompted to enter a string with 4 different letters (z x c v), i have the rest of my program running but i want it to restart program over again if a letter other than those 4 are entered. how would i do that?

    also i have a 22 else if statements for the other letters so if any of those are entered then the user is told that those letters are invalid (although it continues to go all the way through the program)

    there has got to be a better way to do that other than creating those 22 else if statements... anyone know a better way?


  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: need help looping with a string

    Instead of testing if a letter entered is invalid, test to see if it is valid. If it isn't, you obviously have an invalid letter. This technique will also work if the user decided to type say the number 1.

    What you could is is known as a "one and a half" while loop. Basically, you have your input statement asking the user for their letter. While their input isn't one of those 4 letters, keep looping through the while loop until they do input a valid letter.

    // TODO: ask user for letter input
    while(/*TODO: condition test to see if letter is not z, x, c, or v*/)
    {
        System.out.println("Invalid input!");
        // TODO: re-ask user for letter input
    }

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

    Default Re: need help looping with a string

    Ok, so first you are wanting to know how to make the program continue while invalid letters are entered?

    So, you could create a WHILE Loop that checks if the values are invalid. This is an example of code doing the same thing, but with numbers instead of Strings (we cant do everything for you):
    import java.util.Scanner;
     
    public class CheckNumbersProgram 
    {
        public static void main(String[] args) 
        {
        	//Create Scanner
        	Scanner scanner = new Scanner(System.in);
        	//Prompt User
        	System.out.println("Enter 4 Numbers:\n");
        	//Create Variables to Hold Numbers
        	int num1=0;
        	int num2=0;
        	int num3=0;
        	int num4=0;
        	//Set values for User's First Input
        	num1 = scanner.nextInt();
        	num2 = scanner.nextInt();
        	num3 = scanner.nextInt();
        	num4 = scanner.nextInt();
        	/* WHILE Loop to continue prompting user
        	 * This WHILE Loop reads: 
        	 * If Num1 is Negative, Enter Loop, OR
        	 * If Num2 is Negative, Enter Loop, OR
        	 * If Num3 is Negative, Enter Loop, OR
        	 * If Num4 is Negative, Enter Loop
        	 * Else, Skip
        	 */
        	while(num1 < 0 || num2 < 0 || num3 < 0 || num4 < 0)
        	{
        		//Prompt User to Input New Numbers
        		System.out.println("Invalid Numbers Entered. Please Enter 4 New Numbers:\n");
        		//Set values for User's First Input
        		num1 = scanner.nextInt();
        		num2 = scanner.nextInt();
        		num3 = scanner.nextInt();
        		num4 = scanner.nextInt();
        	}
        	System.out.println("Program Finished.");
        }  
    }

    Now, as for checking the letters, instead of checking all letters, you just have to check to see if the letters are NOT the acceptable 4. So, instead of your 22 if statements, we need 4, or even 1 will do.
    4 If Statements:
    if(!input.equals("z"))
    {...}
    else if(!input.equals("x"))
    {...}
    else if(!input.equals("c"))
    {...}
    else if(!input.equals("v"))
    {...}
    1 If Statement:
    if(!input.equals("z") || !input.equals("x") || !input.equals("c") || !input.equals("v"))
    {...}

Similar Threads

  1. Looping with an ArrayList
    By EmSaint in forum Loops & Control Statements
    Replies: 3
    Last Post: September 23rd, 2010, 12:06 AM
  2. Looping through Tokens
    By Viking N7 in forum Loops & Control Statements
    Replies: 2
    Last Post: September 12th, 2010, 12:06 PM
  3. Looping Question
    By miss confused in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 30th, 2010, 12:46 PM
  4. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM