0

So I'm guessing that the solution to this is going to be really simple but I have no idea what I'm looking for so I'd like some help. What happens is that when I run the program, and choose case 1. It prints both "dog's name" and "dogs race" without giving me a chance to fill in the dogs name. So when I choose case 1 I start out only getting to fill in dogs race, how heavy, and how old it is! here is the code I'm using...

do {
        System.out.println("(1 - reg\n2 - tail\n3- delete\n4-exit\nEnter number: ");

        // so this is where the switch stuff starts
        int option=sc.nextInt();
        switch (option) {



        case 1: System.out.println("Dog's Name: ");
            String na=sc.nextLine();

            System.out.println("Dog Race: ");
            String ra=sc.nextLine();

            System.out.println("How heavy?");
            double wey=sc.nextDouble();

            System.out.println("How old?");
            double ag=sc.nextDouble();

            dog doggy= new dog(na, ra, wey, ag);
            kennel.add(doggy);
            break;

        case 2: System.out.println("its a tail");
            break;

        case 3: System.out.println("you delete");
            break;

        case 4: System.out.println("QUITTING\n(Data was not saved srry.)");
            play = false;


        default: System.out.println("try again");
            }
        }while(play);
Strelok
  • 50,229
  • 9
  • 102
  • 115
pat
  • 1
  • 1
  • 1
    possible duplicate of [Scanner issue when using nextLine after nextInt](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint) – Paul Bellora Dec 06 '11 at 23:41

3 Answers3

2

I believe you need to call nextLine() after your call to nextInt(), because that hasn't advanced the scanner to the next line yet.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
1

There's a newline reminder from your first sc.nextInt, you can change the delimiter to \n or just call nextLine(); just after reading the option (Using sc.useDelimiter("\n") )

Slash
  • 496
  • 2
  • 8
0

Try:
int option=Integer.parseInt(sc.nextLine());
This has both the effect of advancing the cursor to the next line and getting the typed number.

o_o
  • 516
  • 2
  • 9