user7621056
user7621056

Reputation:

Using exception handling to force user to input a char (letter) instead of String in Java

Say I had some code like:

Scanner scan = new Scanner(System.in);

System.out.print("Please input a letter: ");
char userLetter = nextLine().charAt(0);

I'd like to use exception handling to make sure the user only inputs a letter and nothing more. Is there a good way to do this?

Upvotes: 1

Views: 3688

Answers (5)

Johnson Abraham
Johnson Abraham

Reputation: 361

Exceptions should not be used for circumstances that might happen too often.... Instead, we should use if...else to handle regularly occurring situations...

Why don't you use something like this:-

Scanner input = new Scanner(System.in);
String text = new String();

do {

    System.out.print("Type a character: ");
    text = input.nextLine();

    if(text.length() > 1) {
         System.out.println("Kindly enter only one character...");
    }

} while(text.length() > 1);

Upvotes: 0

Nestor Sokil
Nestor Sokil

Reputation: 2272

If you need to introduce exception handling here is what I would do:

Scanner scan = new Scanner(System.in);
char c;
while(true) {
    System.out.print("Please input a letter: ");
    try {
        String s = scan.next();
        if(s.length() > 1) {
            throw new RuntimeException("Input too long!");
        }
        c = s.charAt(0);
        if (Character.isLetter(c)){
            throw new RuntimeException("Char is not a letter!");
        }

        // here you can break the loop and do whatever

    } catch(RuntimeException re){
        System.out.print(re.getMessage());
        // you can break the loop or try again
    }
}

P.S. In real-world applications using exceptions for controlling the flow of execution is considered a bad practice. So keep in mind that this code should be used only as an exercise.

Upvotes: 1

dumbPotato21
dumbPotato21

Reputation: 5695

You could check the length of the String and if it is greater than 1, :

String s = null;
do{
    System.out.println("Enter Character : ");
    s = scan.next();
    if(s.length()!=1)
       System.out.println("Error");
}while(s.length()!=1);
System.out.println(s.charAt(0));

If needed, you could add another check for Character#isLetter.

Upvotes: 0

Michael Rhöse
Michael Rhöse

Reputation: 117

If the throwing of exception is required you could do something like this:

Scanner scanner = new Scanner(System.in);
String input = scanner.next();

if(input.length() != 1){
    throw new Exception("...");
}

char userLetter = input.charAt(0);

I think this should work.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311528

Frankly, I wouldn't use exceptions in this situation. There's nothing "exceptional" going on - just the user providing the wrong input. You can check it and prompt the use to input something different:

Scanner scan = new Scanner(System.in);

System.out.print("Please input a letter: ");
String line = nextLine();
while (line.length() != 1 || !Character.isLetter(line.charAt(0))) {
    System.out.print("That's not a letter. Please try again: ");
    String line = nextLine();
}

Upvotes: 0

Related Questions