0

I need to create an exception class that will throw an exception when there are spaces in user input for a name, password, etc. (all strings). I have written all the code that I thought was necessary and no matter what I input, the exception is always thrown.

What am I doing wrong?

The following are snippets of code. If the whole program is needed, let me know.

EmptyInputException class:

public class EmptyInputException extends Exception{
public EmptyInputException(){
    super("ERROR: Spaces entered - try again.");
}
public EmptyInputException(String npr){
    super("ERROR: Spaces entered for " + npr + " - Please try again.");
}

}

Here the getInput method where I catch the exception:

 public void getInput() {
    boolean keepGoing = true;

    System.out.print("Enter Name: ");

    while (keepGoing) {

            if(name.equalsIgnoreCase("Admin")){
            System.exit(1);
            }else

        try {
            name = scanner.next();
            keepGoing = false;
            throw new EmptyInputException();

        } catch (EmptyInputException e) {
            System.out.println("ERROR: Please do not enter spaces.");
            keepGoing = true;
        }//end loop
    }
    System.out.print("Enter Room No.:");

    while (keepGoing) {
        if(room.equalsIgnoreCase("X123")){
            System.exit(1);
        }else
        try {
            room = scanner.next();
            if (room.contains(" ")){
                throw new EmptyInputException();
            }else
                keepGoing = false;

        } catch (EmptyInputException e) {
            System.out.println("ERROR: Please do not enter spaces.");
            keepGoing = true;
        }
    }

    System.out.print("Enter Password:");

    while (keepGoing) {
        if(pwd.equals("$maTrix%TwO$")){
            System.exit(1);
        }else
        try {
            pwd = scanner.next();
            keepGoing = false;
            throw new EmptyInputException();
        } catch (EmptyInputException e) {
            System.out.println("ERROR: Please do not enter spaces.");
            keepGoing = true;
        }
    }

}

I feel like I am missing the part where the scanner input should include spaces, such as:

if(name.contains(" "))

and so on...

So far, my output (after entering a name, for example) will say, Error: Please do not put spaces.

рüффп
  • 5,172
  • 34
  • 67
  • 113
kandaspohn
  • 89
  • 1
  • 12
  • Side note: You shouldn't be using exceptions like this. Exceptions are for exceptional conditions, not signaling. User's have a habit of entering bad data, which makes it a non-exceptional condition. Your code, though, your choice. – MadConan Apr 08 '15 at 19:13
  • The only reason I'm doing it this way is because my instructor asked us to.....but I get what you're saying. Thanks! – kandaspohn Apr 08 '15 at 19:28

2 Answers2

1
try {
        name = scanner.next();
        keepGoing = false;
        if(name.contains(" "))
            throw new EmptyInputException();

    }

Should do the trick?

xrdty
  • 886
  • 2
  • 10
  • 22
  • Cool....so I got rid of that error. But now after I enter a name (with or without spaces) it will print: "enter name: Enter room no.: Enter password: " And then prompt me to enter my name again. No thrown exception/message appears. Is there an issue with my loop? – kandaspohn Apr 08 '15 at 19:23
  • Not that I can directly see, are you calling the method getInput() multiple times perhaps? You could try to debug your code aswell http://stackoverflow.com/questions/22131224/how-to-see-the-execution-steps-of-a-java-program/22131370#22131370 – xrdty Apr 08 '15 at 19:30
0

Your guess was right.

    try {
        name = scanner.next();
        keepGoing = false;
        throw new EmptyInputException(); // You're always going to throw an Exception here.

    } catch (EmptyInputException e) {
        System.out.println("ERROR: Please do not enter spaces.");
        keepGoing = true;
    }

Probably careless mistake. Needs a if(name.contains(" ")):D Same thing happened for your password block.

theguywhodreams
  • 315
  • 1
  • 8