mobinblack
mobinblack

Reputation: 55

Java - Scanner not asking for a input

System.out.println("Please enter the amount of money you have here: ");
Scanner have = new Scanner(System.in);
System.out.println("Please enter your starting bet here: ");
Scanner rate = new Scanner(System.in);

int moneyHad = Integer.parseInt(have.next());
int moneyRate = Integer.parseInt(rate.next());
System.out.println(moneyHad + ", " + moneyRate);

There is my code, and this is my output.

Please enter the amount of money you have here: 
Please enter your starting bet here: 1
1
1, 1

As you can see it prints them both before it asks, thats why there was no input for line 1 of the output.

Please help me quickly!

Upvotes: 1

Views: 279

Answers (2)

Lhoussaine
Lhoussaine

Reputation: 59

you need just one scanner object and call nexInt() method to get the followed entry.

Upvotes: 1

Amar Bessalah
Amar Bessalah

Reputation: 119

  • No need to create 2 Scanner Object
  • There is a method that returns int ( scanner.nextInt() ) no need to ParseInt
  • The input is red when you call the scanner.nextInt() not when creating a Scanner Object

Try this :

Scanner scanner = new Scanner(System.in);

        System.out.print("Please enter the amount of money you have here: ");
        int moneyHad = scanner.nextInt();
        System.out.print("Please enter your starting bet here: ");
        int moneyRate = scanner.nextInt();


        System.out.println(moneyHad + ", " + moneyRate);

Upvotes: 2

Related Questions