0

I'm having some issues with Javas scanner, what I'm trying to do is get inputs and put them into objects and loop that until the user says so, everything works fine on the first loop but on the second loop the scanner skips inputting the name.

My code:

        do 
        {
            //Prompts user for object variables
            System.out.println("Please enter the name of person: ");
            Client.SetName(kb.nextLine());
            System.out.println("Please enter the coin value for person: ");
            Client.SetCoins(kb.nextInt());
            System.out.println(Client.GetName() + " " + Client.GetCoins());
            
            
            //Clients.add(Client);
          
            
            //Checks if user wants to loop
            System.out.print("Do you have another client to person (y/n)");
            bool = kb.next().trim().toLowerCase().charAt(0);
         }while(bool != 'n');

Console Log:

Please enter the name of person:

test

Please enter the coin value for person:

5

test 5

Do you have another client to person (y/n)y

Please enter the name of person:

Please enter the coin value for person:

10

10

Do you have another client to person (y/n)y

Please enter the name of person:

Please enter the coin value for person:

test

Exception in thread "main" java.util.InputMismatchException

Chetan Joshi
  • 5,279
  • 4
  • 24
  • 33
BlaM
  • 43
  • 4

1 Answers1

1

Try changing nextLine to next while getting name as input

Seema
  • 84
  • 6