Geneticwarrior
Geneticwarrior

Reputation: 47

How to continue loop after throwing exception?

So here is what I have. I'm working on the super beginnings of a text game, and I want user to move around until he quits. I'm trying to have it throw an exception to tell him his input was not valid, and continue with the loop. Not sure how to do this. Any help appreciated!

import java.util.Scanner;

public class Interface {

public static void main(String[] args) {

    String direction;
    System.out.println ("Welcome to the Land of Poonigeddon!");
    System.out.println ("To move, type one of these four letters: N,S,E,W. or type quit to quit.");
    System.out.println ("This will move you in the corresponding cardinal direction.");
    Scanner input = new Scanner (System.in);
    System.out.println ("");
    System.out.println ("Now, which way will you go?");
    while (true) {
        direction = input.nextLine();

           if (direction.equalsIgnoreCase ("e")) {
               System.out.println ("You have moved to the east.");
           }
           if (direction.equalsIgnoreCase("w")) {
               System.out.println("You have moved to the west.");
           }
           if (direction.equalsIgnoreCase("n")) {
               System.out.println ("You have moved to the north.");
           }
           if (direction.equalsIgnoreCase("s")) {
               System.out.print("You have moved to the south.");
           }
           if (direction.equalsIgnoreCase("quit")) {
               break;
           }
           else //throw new exception? i had illegalargumentexception but     it kicks out
               // of the loop after it catches input that isnt     n,w,e,s,quit("Not a valid movement key, please try again.");


    }
}

}

Upvotes: 0

Views: 306

Answers (3)

arilaan
arilaan

Reputation: 404

This works for me, keep in mind that you would want to use try catches if you don't want to necessarily terminate the program. In this case it makes more sense to just replace your "throw" with System.out.println(). Also don't forget continue statements in your if blocks, otherwise you might run into the else at the end which belongs to the last if.

import java.util.Scanner;

 public static void main(String[] args) {
    System.out.println ("Welcome to the Land of Poonigeddon!");
    System.out.println ("To move, type one of these four letters: N,S,E,W. or type quit to quit.");
    System.out.println ("This will move you in the corresponding cardinal direction.");
    Scanner input = new Scanner (System.in);
    System.out.println ("");
    Boolean playing = true;
    while (playing == true) {
        System.out.println ("Now, which way will you go?"); /*Can go here or in main*/
        String direction = input.nextLine();
        if (direction.equalsIgnoreCase ("e")) {
           System.out.println("You have moved to the east.");
           continue;
        }
        if (direction.equalsIgnoreCase("w")) {
           System.out.println("You have moved to the west.");
           continue;
        }
        if (direction.equalsIgnoreCase("n")) {
           System.out.println("You have moved to the north.");
           continue;
        }
        if (direction.equalsIgnoreCase("s")) {
           System.out.println("You have moved to the south.");
           continue;
        }            
        if (direction.equalsIgnoreCase("quit")) {
           playing = false;
           continue; #these two lines or break work fine
        }
        else {
            System.out.println("Not a valid movement key, please try again.");
        }
    }
}

Upvotes: 0

jester
jester

Reputation: 3489

In such a case, why use exceptions? Just output a message inside the loop. Exceptions are supposed to be used for exceptional situations that might occur during program run.

while (true) {
    direction = input.nextLine();

       if (direction.equalsIgnoreCase ("e")) {
           System.out.println ("You have moved to the east.");
       }
       if (direction.equalsIgnoreCase("w")) {
           System.out.println("You have moved to the west.");
       }
       if (direction.equalsIgnoreCase("n")) {
           System.out.println ("You have moved to the north.");
       }
       if (direction.equalsIgnoreCase("s")) {
           System.out.print("You have moved to the south.");
       }
       if (direction.equalsIgnoreCase("quit")) {
           break;
       }
       else
           System.out.print("Not a valid movement key, please try again.");
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209004

In your case, you don't need to catch anything. Just add the System.out.println(question) inside the loop. And for the esle, your error message.

Also you should consider using else if instead of a bunch of if

while (true) {
       System.out.println ("Now, which way will you go?");
       direction = input.nextLine();

       if (direction.equalsIgnoreCase ("e")) {
           System.out.println ("You have moved to the east.");
       }
       else if (direction.equalsIgnoreCase("w")) {
           System.out.println("You have moved to the west.");
       }
       else if (direction.equalsIgnoreCase("n")) {
           System.out.println ("You have moved to the north.");
       }
       else if (direction.equalsIgnoreCase("s")) {
           System.out.print("You have moved to the south.");
       }
       else if (direction.equalsIgnoreCase("quit")) {
           break;
       }
       else {
           System.out.println("Not a valid movement key, please try again.")
       }
}

Upvotes: 1

Related Questions