Timothy Portelance
Timothy Portelance

Reputation: 9

How to throw an exception if user input is anything but string?

My problem is that I need to throw an exception when the user inputs anything other than an alphabet.

I can't change the fact that I'm using BufferedReader because it's part of an assignment for school. Here is my code:

public static String phoneList(String lastNameInput, String nameInput)
        throws IOException {

    BufferedReader bufferedreader = new BufferedReader(
            new InputStreamReader(System.in));

    try {

        System.out.println("Please input your first name.");
        // User input block
        String input = bufferedreader.readLine();
        nameInput = input;
    } catch (IOException e) {
        System.out.println("Sorry, please input just your first name.");
    }

    try {
        System.out.println("Please input your last name.");
        String input2 = bufferedreader.readLine();
        lastNameInput = input2;
    } catch (IOException e) {
        System.out
                .println("Sorry, please only use letters for your last name.");
    }
    return (lastNameInput + ", " + nameInput);

}

So what method can I use to throw an exception if the user input contains a number, or non-alphabet character?

Upvotes: 0

Views: 11987

Answers (2)

PermGenError
PermGenError

Reputation: 46398

If you mean that the String should only contain alphabets, then use String.matches(regex).

if(bufferedreader.readLine().matches("[a-zA-Z]+")){
System.out.println("user entered string");
}
else {
throw new IOException();
}

"[a-zA-Z]" regex only allows alphabets from a-z or A-Z

or if you dont wanna go with regex. you wouldhave to loop thru the String and check each character if it is not a number.

try{

        System.out.println("Please input your first name.");
        //User input block
        String input = bufferedreader.readLine();
        nameInput = input;
         for(int i=0; i<nameInput.length();i++){
             if(Character.isLetter(nameInput.charAt(i))){
                continue;
              }
              else {
                throw new IOException();
              }
           }
        } catch(IOException e){
            System.out.println("Sorry, please input just your first name.");
        }

Upvotes: 3

Stephen C
Stephen C

Reputation: 718718

My problem is that I need to throw an exception when the user inputs anything other than string (i.e, an int, float, or double.)

What you are asking doesn't make sense. To illustrate, "12345" is a string. Yes ... IT IS. So if you call readLine() and the line consists of just digits, you will get a string consisting of just digits.

So to solve your problem, after you have read the string, you need to validate it to make sure that it is an acceptable "first name". You could do this a number of ways:

  • The crudest way is to iterate over the string, checking that each character us acceptable.
  • A slightly less crude (but probably wrong) way might be to try to parse the string as an integer of floating point number. (As an exercise, figure out why I said "probably wrong".)
  • The elegant way would be to use java.util.regex.Pattern and a pattern that matches acceptable names, and excludes unwanted stuff like digits, embedded white-space and punctuation.

And as @DanielFischer's comment points out, you need to think carefully about what characters should be acceptable in names. Accents are one example, and others might be cyrillic or chinese characters ... or hyphens.

Upvotes: 3

Related Questions