3 Replies - 789 Views - Last Post: 04 October 2012 - 09:02 AM Rate Topic: -----

#1 elliotmersch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Do While loop won't pause for input

Posted 04 October 2012 - 08:44 AM

At the end of my initial do while loop, I want it to prompt the user if they want to play again, but instead of pausing the loop and allowing the user to input yes or no, it automatically inputs nothing and outputs "Invalid input." (default case on my switch).

// Elliot Mersch
// 10/3/12
// Dice.java

import java.util.Scanner;
import java.util.*;


public class Dice
{

	public static void main(String[] args)
	{
	
		Scanner keyboard = new Scanner (System.in);
	
		int guess;
		int rollA, rollB, sum;
		int count = 0;
		String play;
		boolean playBool = true;
		
		Roll myRoll = new Roll();
		
		
		do{
			System.out.println("Dice Game");
			System.out.println("Enter a number 2-12: ");
			guess = keyboard.nextInt();
			
			do{
				count++;
				rollA = myRoll.getRollA();
				rollB = myRoll.getRollB();
				sum = rollA + rollB;
				
				System.out.println("Roll 1: " + rollA);
				System.out.println("Roll 2: " + rollB);
				System.out.println("Sum: " + sum);
				
				if (sum == guess)
					{
						System.out.println("Match!");
					}
				else
					{
						System.out.println("No match.");
					}
				}
				while(count < 3 || guess != sum);
			

			System.out.println("Do you want to play again? (yes/no): ");
			play = keyboard.nextLine();
			
			switch (play){
			case "yes": playBool = true;
			case "no": playBool = false;
			default: System.out.println("Invalid input.");
							}
	
			}	
			while(playBool == true);
			
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Do While loop won't pause for input

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: Do While loop won't pause for input

Posted 04 October 2012 - 08:51 AM

That's because you made these tow consecutive calls:
scanner.nextInt()
...
scanner.nextLine()



The problem is because you used nextInt(), which does not also "eat up" the "enter" character (\n or \r or \n\r) that you get when submitting you answer. You need to do this:

guess = keyboard.nextInt();
keyboard.nextLine(); // Eat up the linefeed



And then it will work.
Was This Post Helpful? 1
  • +
  • -

#3 elliotmersch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: Do While loop won't pause for input

Posted 04 October 2012 - 08:59 AM

Worked like a charm. Thanks for the help and quick response man.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: Do While loop won't pause for input

Posted 04 October 2012 - 09:02 AM

You're very welcome. Glad I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1