0

I'm having an issue with the while loop repeating after the user replies yes, which should repeat the program, but instead it skips through the body and immediately asks the last question again.

Here is a portion of the program (Type in amphibian when it asks for the monster class):

public class MH4 {

    public static void main(String[] args) {

        String answer = "yes";
        String monster, amphibian;
        String s2 = "Water", s3 = "Thunder";
        String b1 = "Head", b2 = "Front legs", b4 = "Hind legs";
        String f3 = "Fatigue", i1 = "Ice Blight", i2 = "Severe Ice Blight";



        Scanner keyboard = new Scanner(System.in);  

        while (answer.equalsIgnoreCase("yes")) {

            System.out.println("Which monster class do you want to look up?");

            monster = keyboard.nextLine();

            if (monster.equalsIgnoreCase("Amphibian")){

                System.out.println("\nThese are the monsters under that classification:\n");

                System.out.println("Tetsucabra\nBerserk Tetsucabra\nZamite\nZamtrios\nTigerstripe Zamtiros");

                System.out.println("\nWhich of these monsters would you like to gather information on?");

                amphibian = keyboard.nextLine();

                if (amphibian.equalsIgnoreCase("Tetsucabra"))

                    System.out.println("\nThis monster is weakest to: " + s2 + " and " + s3);

                System.out.println("\nThis monster's weakest point is its: " + b1 + ", " + b2 + ", and " + b4);

                System.out.println("\nThis monster can cause:\n" + f3 + "\n" + i1 + " *\n" + i2 + " *\n");                          

                System.out.println("* Tetsucabra only inflicts these while in the frozen seaway");

                System.out.println("\nImportant Notes:\n- Its tail becomes its weakest point while it is holding boulders.\n- The projectiles that it shoots drain stamina.");
            }

            else { 
                System.out.println("That monster class does not exist in this game.");
            }

            System.out.println("\nIs there another monster or monster class you wish to look up?");

            answer = keyboard.next();
        }
    }
}

If you can figure out the error please let me know, as I have no idea what it could be.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Hoot
  • 7
  • 2
  • Any reason why you've decided to use `next()` instead of `nextLine()` at the bottom? – Makoto Mar 16 '15 at 21:47
  • In short, `next()` doesn't consume line separator. So for data like `foo(enterKey)` input will look like `foo\n`. `next` will consume `foo` part, but there will be one `\n` left in input so `nextLine` will treat it as empty line. Use `nextLine` instead of `next` to solve this problem. – Pshemo Mar 16 '15 at 21:49
  • Also you probably should add `{` and `}` after `if (amphibian.equalsIgnoreCase("Tetsucabra"))` if you want execution of more than one instruction to be dependant on this `if`. – Pshemo Mar 16 '15 at 21:51
  • @Pshemo: I'm thinking that this is a typo more than anything else. Their intent wasn't to use `next`, but `nextLine()`. – Makoto Mar 16 '15 at 21:51
  • Try debugging it line by line with Eclipse/etc..., for these simple examples you'll learn a lot more than asking here. Read this: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – uraimo Mar 16 '15 at 21:52
  • @Makoto You are probably right. – Pshemo Mar 16 '15 at 21:55

0 Answers0