Continue if it is a letter

0

Good morning, I was wondering if it is possible to force the user to put a letter or not advance. With an int it would be something like this:

int x = 0;
while (x < 1){
    if(lector.hasNextInt){
        int numero = lector.nextInt();
        x++;
    }
}

I've been looking for how to do it with a letter but I can not find it.

Thank you.

    
asked by Jordi 03.01.2018 в 21:43
source

2 answers

0

Here I leave a code that I just did, I hope it serves you, it's a shabby but fast and effective way

public static void main(String[] args) {
    boolean salir=false;
    Scanner input = new Scanner(System.in);
    while(salir == false) {
        System.out.println("Pulsa y para continuar");

        char letraUsuario = input.next().toLowerCase().charAt(0);

        if(letraUsuario == 'y') {
            System.out.println("Puedes continuar");
            salir = true;
        }else {
            System.out.println("La letra no es adecuada, vuelve a introducir una letra");
        }


    }
}
    
answered by 03.01.2018 / 22:00
source
0

You can compare against a regular expression

br = new BufferedReader(new InputStreamReader(System.in));
String sTexto;
String pattern = "[a-zA-Z]";
do {
  System.out.println("Ingrese una letra");
  sTexto = br.readLine();
} while(!sTexto.matches(pattern));
    
answered by 05.01.2018 в 22:29