Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: java.util.InputMismatchException error with Boolean values

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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!

    Maybe I could put the boolean value into a string and say:

    if (playAgain=="yes") {
    playAgain=true
    break;
    }
    else {
    playAgain=false
    break;
    }

    Here is the actual code:

    import java.util.Scanner;
     
    public class EightTorches {
     
    	public static void 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;
    		    }
    			else if (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;
    				}
    				else if (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;
    			  }
    			else if (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;
    				   }
    				else if (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;
    			  }
    			else if (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;
    				   }
    				else if (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;
    			  }
    			else if (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.

    Advice?

    -Silent


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java.util.InputMismatchException error with Boolean values

    You don't break out of if/else clauses.

    Break is used for escaping looping structures or switch cases.

    So when you add a break, you're telling Java that you want to break out of the current loop structure or switch case (in your case, the while loop which repeats the game).

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: java.util.InputMismatchException error with Boolean values

    So I should delete the breaks?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,941
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: java.util.InputMismatchException error with Boolean values

    If you want to stay in the loop, delete the breaks.
    If you want to exit the loop, leave them in.

  5. #5
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: java.util.InputMismatchException error with Boolean values

    Alright. Thanks

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  2. using java.util.InputMismatchException with try
    By itayj in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 26th, 2011, 07:24 PM
  3. Replies: 3
    Last Post: April 26th, 2011, 03:51 AM
  4. Replies: 4
    Last Post: April 29th, 2009, 11:53 AM
  5. Program to populate all array values using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 7th, 2009, 06:31 AM