string validation only alphanumeric

0

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?

    
asked by 24.09.2017 в 06:09
source

1 answer

1

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"); 
    }
}
    
answered by 24.09.2017 в 06:39