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?
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.
Code Java:
// 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
}
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):
Code java:
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:
Code java:
if(!input.equals("z"))
{...}
else if(!input.equals("x"))
{...}
else if(!input.equals("c"))
{...}
else if(!input.equals("v"))
{...}
1 If Statement:
Code java:
if(!input.equals("z") || !input.equals("x") || !input.equals("c") || !input.equals("v"))
{...}