The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

java.util.InputMismatchException error with Boolean values

Hello!

I recently was inspired by my friend's inspiration of someone he knew to write a code that simulated a game that was created by my friend's friend in Minecraft. *whew*. The game was supposed to have eight torches lined up in a row, and you were supposed to choose a number between 1 and 3. The computer would also choose a number between 1 and 3, and the total number of torches would be lit up. You would continue in this way until you either hit eight torches lit exactly, or you went over eight torches lit, in which case you would lose.

The code itself works fine, except for the fact that as soon as I was finished, I decided to impress my friend by adding the function of the the computer asking the user if he/she wants to play again.

Not so simple.

After I had finally gotten the code running after copying and pasting the code that asks the user a question and adding the break; to it, the IDE terminates the code if I enter true or false!

importjava.util.Scanner;publicclass EightTorches {publicstaticvoid main(String[] arg){int com1, com2, com3, com4;int num1, num2, num3, num4;int totalTorches;boolean playAgain=true;
Scanner scan =new Scanner(System.in);do{System.out.print("This is a game where there are eight torches. You pick a number between 1 and 3 and the computer picks a number. That total number of torches is lit up. I f you light exactly eight torches, you win! However, if you light more than eight torches, you lose. The game will continue to ask for numbers until you have either won or lost. Have fun!");System.out.println();System.out.println();System.out.print("What is you first guess? ");
com1=(int)(Math.random()*3)+1;
num1= scan.nextInt();
totalTorches=num1+com1;if(totalTorches==8){System.out.print("The total number of torches is equal to eight! You win!");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches>8){System.out.print("You have too many torches. You lose.");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches<8){System.out.print("Eight torches have not been lit up yet.");System.out.println();System.out.println();System.out.print("What is your next guess?");}
com2=(int)(Math.random()*3)+1;
num2= scan.nextInt();
totalTorches=num2+com2+num1+com1;if(totalTorches==8){System.out.print("The total number of torches is equal to eight! You win!");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches>8){System.out.print("You have too many torches. You lose.");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches<8){System.out.print("Eight torches have not been lit up yet.");System.out.println();System.out.println();System.out.print("What is your next guess? ");}
com3=(int)(Math.random()*3)+1;
num3= scan.nextInt();
totalTorches=num3+com3+num2+com2+num1+com1;if(totalTorches==8){System.out.print("The total number of torches is equal to eight! You win!");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches>8){System.out.print("You have too many torches. You lose.");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches<8){System.out.print("Eight torches have not been lit up yet.");System.out.println();System.out.println();System.out.print("What is your next guess? ");}
com4=(int)(Math.random()*3)+1;
num4= scan.nextInt();
totalTorches=num4+com4+num3+com3+num2+com2+num1+com1;if(totalTorches==8){System.out.print("The total number of torches is equal to eight! You win!");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}elseif(totalTorches>8){System.out.print("You have too many torches, or you did not equal eight. You lose.");System.out.println();System.out.println();System.out.print("Would you like to play again? ");
playAgain= scan.nextBoolean();break;}}while(playAgain =true);}}

The boolean value is supposed to tell the computer whether or not to replay the do..while loop.