Home > Software engineering >  My Exception is not working even i already created an If else statement for it
My Exception is not working even i already created an If else statement for it

Time:11-11

So I made an exception on another class called EmptyInputException, so basically the purpose of this is to inform the user if they enter a space in their output. Any thoughts why it's not working for me. The EmptyInputException, it's meant for the users if they accidentaly input a space instead of letters then it should print the message from the exception but in my case it's not working. When i try to input a blank answer there is no print message from the EmptyInputException

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    String questions[] = {"What is the color of the sky? ", "What is 1   1? ",
            "What is the capital of the Philippines? ", "Who is the current president of the Philippines? ",
            "What is the capital of Japan? ", "What is 2   3? ",
            "What is 9   1?", "What is the capital of the United States? ",
            "What is 10   10? ", "How many hand fingers do humans have? "};

    String choices[] = {"a","b","c"};

    int x = 0;
    int y = 0;
    

    try {

        while(x<=9) {
            System.out.println("Choose a Letter: a , b , c");
            System.out.println("No."   (x 1)   " "   questions[x]);
            String answer = scan.next();
            x  ;
            
            if(answer.equals(choices[0])) {
                scan.nextLine();

            } else if (answer.equals(choices[1])) {
                scan.nextLine();

            } else if (answer.equals(choices[2])) {
                scan.nextLine();

            } else if (answer.equals(" ")) {
                throw new EmptyInputException();

            } else {
                throw new InvalidLetterException();
            }   
    
        } 

    } catch(InvalidLetterException e) {
        System.out.println(); //Spacing
        System.out.println(e.getMessage());
        System.out.println(); //Spacing
        System.out.println("You can try again.");
        System.out.println(); //Spacing
        do {
            System.out.println("No."   (y 1)   " "   questions[y]);
            scan.next();
            y  ;
        }while(y<=9);
        
    } catch (EmptyInputException e) {
        System.out.println(e.getMessage());
    }



}

public class EmptyInputException extends Exception {

//QuizBee.java
public EmptyInputException() {
    super("ERROR : Please do not enter spaces.");
}

public EmptyInputException(String message) {
    super(message);
}

}

CodePudding user response:

You are creating a Scanner with the default token delimiter, which its documentation states

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

And you are using the next() method to read in user input. That method works as follows according to its official Documentation:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

The next() method uses the delimiter, which is whitespace in your case, to determine where an input begins and ends. That means that with whitespace as a delimiter, the next() method will and can never return a whitespace itself, because that is the used delimiter.

You could try playing around with your delimiter, but I'd rather suggest just using nextLine() to read in your input and then check if result is blank to determine the user entered nothing or just whitespaces:

  String answer = scan.nextLine();
  if (answer.isBlank()) {
      throw new EmptyInputException();
  }
  

CodePudding user response:

From the information you gave @OH GOD SPIDERS is very likely right. To see more evidence, change

System.out.println(e.getMessage());

into

System.out.println(e.getClass().getName()   " "   e.getMessage());

or more easily

e.printStackTrace(System.out);

Once you know your code works you can still tweak the information that will be presented to the user.

CodePudding user response:

The problem is in the Scanner class. It returns tokens but not the token delimiters. And per default (you did not configure anything else) the delimiters will match any whitespace.

Thus the line

String answer = scan.next();

will not return if only linefeeds or space characters are entered, and all the rest of your handling code goes unused.

To get out of that, configure the scanner to only use carriage returns as delimiters.

  •  Tags:  
  • java
  • Related