How can I verify if, when entering a string (String), I only recognize characters of the alphabetic type along with their line break and if they are entered with numbers indicated on the screen?
How can I verify if, when entering a string (String), I only recognize characters of the alphabetic type along with their line break and if they are entered with numbers indicated on the screen?
You can use regular expressions to validate strings.
import java.util.Scanner;
import java.util.regex.Pattern;
class Test {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
System.out.print("Introduce texto: ");
String input = scan.nextLine();
boolean alfa = Pattern.matches("^[a-zA-Z]*$", input);
// Si son solo letras imprime "Alfabetico" si no imprime "No Alfabetico"
System.out.println(alfa ? "Alfabetico" : "No Alfabetico");
}
}