Hello,I have a user input and trying to write a precondition that will give an error if the user inputs number or some other odd character (except ' and .)
I am thinking of writing and if statement that if my search method finds anything like that it will prompt user to re-enter. The problem is i dont know how to implement the search method. Can you help me?

here is a small piece of my code:
[ System.out.println("Please enter name of the employee:");
name = stdin.readLine();
if ("search will find numbers or weird characters in the input")
then
{

}
]

Recommended Answers

All 2 Replies

For testing whether or not a Character is a digit, you can use the Character class's method isDigit(). So, for example,

String s = stdin.readLine();
for(int i = 0; i < s.length(); i++){
    if (s.charAt(i).isDigit()){
       System.out.println("It's a digit!");
    }
}

But for your purposes, if you don't want any "weird" characters in your String, you should check to see if each character is anything other than a-z, A-Z, or ' and any other character that are allowed. This will obviously mean less code and more efficient code. Java uses the Unicode character set, so look into that to figure out how to code something that will basically say

if (myCharacter < a || myCharacter > z)

Thanks Bunch!
Very explicit and efficient reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.