1

I am creating a random name generator for a fantasy table top game I play with my friends. Everything is working so for, except for when I go to scan a nextLine() for my description, the program doesn't wait for input, and instead goes back to the start of the while loop. What I want to do, is be able to type in a short sentence about the character they are interacting with, and have it set to the String value Description, later my goal is to print these values out in a text document as I create them for future use. My code for that section looks like as follows. Any help is greatly appreciated.

    System.out.println("Male (m) Female (f) Quit (q)");
    ui = UserInput.next();
    while(!(ui.equals("q"))){
        if(ui.equals("m")){
            mName = maleNameGenerator(MS, SS, MaleNamesArray, SurnamesArray);
            System.out.println(mName);
            System.out.print("Location: ");
            Location = UserInput.next();
            System.out.print("Profession: ");
            Profession = UserInput.next();
            System.out.print("Description: ");
            Description = UserInput.nextLine();
        }
        else if(ui.equals("f")){
            fName = femaleNameGenerator(FS, SS, FemaleNamesArray, SurnamesArray);
            System.out.println(fName);
        }
        else if(!(ui.equals("q"))){
            System.out.println("Please input valid choice.");
        }
        System.out.println("Male (m) Female (f) Quit (q)");
        ui = UserInput.next();
        ui.toLowerCase();
    }
    UserInput.close();
  • possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Tom Jul 26 '15 at 22:31
  • What is the code of `UserInput`? You talk about a `Scanner` here but there's no scanner in the code you've provided. For better help sooner, post a [mcve] to make the problem more easily reproducible for those seeking to help you. – durron597 Jul 26 '15 at 22:37
  • 2
    You should respect the conventional CamelCase. For one moment, I thought `UserInput` was a class whereas I now understand it's just an instance of `Scanner`. Using CamelCase will make your programs more readable for every programmer – Dici Jul 26 '15 at 22:49
  • @durron597 it must be an instance of `Scanner`, but I have first had the same reaction as you – Dici Jul 26 '15 at 22:50
  • @Dici Oh, he used a capitalized variable name for something not a class. Duh. – durron597 Jul 26 '15 at 22:52

1 Answers1

2

The next() method leaves behind a newline character after taking input. In your code, the nextLine() method simply consumes this newline character and ignores the actual input. You can add another nextLine() to make sure the input is received.

...
System.out.print("Description: ");
UserInput.nextLine();
Description = UserInput.nextLine();

This problem can also occur when using other non-String type Scanner input methods (nextInt(), nextDouble(), etc). The explanation and solution would be the same.

deezy
  • 1,460
  • 1
  • 9
  • 22