1

In my program below I want the user to enter 3 letters only. My program detects a an incorrect input but does not continually prompt the user to enter the correct input. It instead moves on to the 2nd requested input if a number is entered instead of a letter for example.

My question is how can I make this program loop until the correct input is entered?

Also can my code be simplified? As I have the same conditions for input1 input2 and input3 is there a way to specify the conditions in a single statement rather than what I did?

public static void main(String[] args) 

{
    Scanner scnObj = new Scanner(System.in);
    System.out.println("enter 3 letters");
    String input1 = scnObj.nextLine();
    if(!Pattern.compile("[a-zA-Z]*").matcher(input1).matches()){
        System.out.println("try again.");
    }
    else {
        System.out.println("enter the 2nd");
    }
    String input2 = scnObj.nextLine();
    if(!Pattern.compile("[a-zA-Z]*").matcher(input2).matches()){
        System.out.println("Please try again.");
    }
    else {
        System.out.println("enter the 3rd");
    }
    String input3 = scnObj.nextLine();
    if(!Pattern.compile("[a-zA-Z]*").matcher(input3).matches()){
        System.out.println("try again.");
    }
    scnObj.close();
}

}

3 Answers3

0

try using do - while statement:

String input1;    
do {
    System.out.println("enter 3 letters");
    input1 = scnObj.nextLine();
} while(/*your statement*/);

If I got you right, this will force the user to enter said 3 letters. Sorry if I didn't understand your question.

rkosegi
  • 12,129
  • 5
  • 46
  • 75
Pavel100J
  • 1
  • 2
0

you can use while loop

 public static void main(String[] args) throws IOException {
        Scanner scnObj = new Scanner(System.in);
        System.out.println("enter 3 letters");
        String input1 = scnObj.nextLine();
        String outPut="";
        while(!input1.chars().allMatch(Character::isLetter) || outPut.length() < 3){
            if(input1.chars().allMatch(Character::isLetter) && (input1.length() + outPut.length() <= 3)){
                outPut = outPut + input1;
                if(outPut.length() == 3){
                    break;
                }
            }else if(input1.chars().allMatch(Character::isLetter) && input1.length()==3){
                outPut = input1;
                break;
            }
            System.out.println("try again.");
            input1 = scnObj.nextLine();
        }
        System.out.println(outPut);
        scnObj.close();
}
dassum
  • 3,463
  • 2
  • 14
  • 27
0

Do it as follows:

import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner scnObj = new Scanner(System.in);
        System.out.println("enter 3 letters");
        String input1;
        do {
            input1 = scnObj.nextLine();
            if ((!Pattern.compile("[a-zA-Z]*").matcher(input1).matches())) {
                System.out.println("try again");
            }
        } while (!Pattern.compile("[a-zA-Z]*").matcher(input1).matches());

        System.out.println("enter the 2nd");
        String input2;
        do {
            input2 = scnObj.nextLine();
            if ((!Pattern.compile("[a-zA-Z]*").matcher(input2).matches())) {
                System.out.println("try again");
            }
        } while (!Pattern.compile("[a-zA-Z]*").matcher(input2).matches());

        System.out.println("enter the 3rd");
        String input3;
        do {
            input3 = scnObj.nextLine();
            if ((!Pattern.compile("[a-zA-Z]*").matcher(input3).matches())) {
                System.out.println("try again");
            }
        } while (!Pattern.compile("[a-zA-Z]*").matcher(input3).matches());

        scnObj.close();
    }
}

A sample run:

enter 3 letters
1
try again
2
try again
a
enter the 2nd
w
enter the 3rd
4
try again
5
try again
p
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72