Limit user input to alphanumeric values

Basically, my situation requires me to check to determine if a user-defined string from the keyboard is only in alphabetical order in one case and only numbers in another case. It is written in Java.

my current code is:

switch (studentMenu) {
                case 1: // Change all four fields
                    System.out.println("Please enter in a first name: ");

                    String firstNameIntermediate = scan.next();
                    firstName = firstNameIntermediate.substring(0,1).toUpperCase() + firstNameIntermediate.substring(1);
                    System.out.println("Please enter in a middle name");
                    middleName = scan.next();
                    System.out.println("Please enter in a last name");
                    lastName = scan.next();
                    System.out.println("Please enter in an eight digit student ID number");
                    changeID();
                    break;
                case 2: // Change first name
                    System.out.println("Please enter in a first name: ");
                    firstName = scan.next();
                    break;
                case 3: // Change middle name
                    System.out.println("Please enter in a middle name");
                    middleName = scan.next();
                    break;
                case 4: // Change last name
                    System.out.println("Please enter in a last name");
                    lastName = scan.next();
                case 5: // Change student ID:
                    changeID();
                    break;
                case 6: // Exit to main menu
                    menuExit = true;
                default:
                    System.out.println("Please enter a number from 1 to 6");
                    break;
            }
        }
    }

public void changeID() {
    studentID = scan.next();
    }

I need to make sure that StudentID is only numeric, and each of the name segments is in alphabetical order.

+3
source share
6 answers

java.util.Scannercan already check if the next token has a given pattern / type using methods hasNextXXX.

boolean hasNext(String pattern), , , [A-Za-z]+:

    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter letters:");
    while (!sc.hasNext("[A-Za-z]+")) {
        System.out.println("Nope, that not it!");
        sc.next();
    }
    String word = sc.next();
    System.out.println("Thank you! Got " + word);

:

Please enter letters:
&#@#$
Nope, that not it!
123
Nope, that not it!
james bond
Thank you! Got james

, - , int, hasNextInt(), nextInt().

+5

, , . :

import java.util.regex.*;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        System.out.println(isNumeric("123"));
        System.out.println(isNumeric("abc"));
        System.out.println(isNumeric("abc123"));

        System.out.println(isAlpha("123"));
        System.out.println(isAlpha("abc"));
        System.out.println(isAlpha("abc123"));
    }

    private static final Pattern NUMBERS = Pattern.compile("\\d+");
    private static final Pattern LETTERS = Pattern.compile("\\p{Alpha}+");

    public static final boolean isNumeric(String text)
    {
        return NUMBERS.matcher(text).matches();
    }

    public static final boolean isAlpha(String text)
    {
        return LETTERS.matcher(text).matches();
    }
}

, "getAlphaInput" "getNumericInput", // , . , , getInput(Pattern), .

, "" - az AZ... .., Pattern .

, , . .

+4

, , Character.isDigit() Character.IsLiteral() mabybe :

for( char c : myString.toCharArray() ) {
    if( !Character.isLiteral(c) ) {
        // 
    }
}
0

, , . . , - ( , ):

while (!firstName.matches("^[a-zA-Z]+$")) {
    System.out.println("Please enter in a first name");
    firstName = scan.next();
}

...

while (!studentID.matches("^\\d{8}$")) {
    System.out.println("Please enter in an eight digit student ID number");
    changeID();
}

, , .

"Regex" , , .

0

try regexp: \ d + - numeric, [A-Za-z] + - alphabetical

0
source

This is the code

    public class InputLetters {
    String  InputWords;
    Scanner reader;
    boolean [] TF;
    boolean FT;     

    public InputLetters() {

        FT=false;

        while(!FT){     
            System.out.println("Enter that you want to: ");
            reader = new Scanner(System.in);
            InputWords = reader.nextLine();
            Control(InputWords);            
        }

    }


public void Control(String s){
String [] b = s.split(" ");
TF = new boolean[b.length];
    for(int i =0;i<b.length;i++){
        if(b[i].matches("^[a-zA-Z]+$")){
            TF[i]=true;
        }else
        {
            TF[i]=false;
        }               
    }
    for(int j=0;j<TF.length;j++){
        if(!TF[j]){
            FT=false;
            System.out.println("Enter only English Characters!");
            break;
        }else{
            FT=true;
        }

    }
}
0
source

Source: https://habr.com/ru/post/1751144/


All Articles