MooseReic
MooseReic

Reputation: 41

Checking character properties in Java

Alright, I'm coding this program that will discard any characters that are not letters. And right now I am having trouble trying to have the program identify which is which. Here's some of the code I did.

System.out.println("Press enter every time, you type a new word, and press the period button to end it.");
Scanner question = new Scanner(System.in);
System.out.println("Press enter to continue, or tupe something random in");
String userInput = question.next();
while(!userInput.equals(".")){
    String userInput2 = question.next();
    System.out.println(userInput2);

    if(userInput2.equals("Stop")){
        break;
    }
}

Upvotes: 0

Views: 101

Answers (2)

Konstantin Pavlov
Konstantin Pavlov

Reputation: 985

Go through the string and call Character.isLetter(char) for each char to test if it is a letter character.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521239

You can use a regular expression to remove all characters which are not either lowercase or uppercase letters:

String userInput2 = question.next();
userInput2 = userInput2.replaceAll("[^a-zA-Z]", "");
System.out.println(userInput2);

Upvotes: 1

Related Questions