Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Loop not repeating correctly

    This is only a portion of my program, but the main issue has to do with the loop not repeating correctly. I'm using a try and catch block with a do while to validate my input for my addition problem. What it's suppose to do is when a user inputs letters with numbers or just letters its suppose to say "Sorry, try again" and then let the user input the answer for the said math problem. The issue I am getting is when i input numbers it's fine. Even if its not the correct number. But when I input a single letter it says "Sorry, try again " and gives me a new problem. It's suppose to let the user try again and answer the same problem.

    I've only been working with java for a month or so this is still confusing for me. Plus, I've been at this for more than 12 hours and I wasn't going to ask for help, but I'm at my wits end. So any help or suggestions would be grateful.

    Not sure if I mentioned it earlier, but the problem is in the Addition method, not the MainMenu. Just wanted to clarify that and be transparent.


    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    import java.util.InputMismatchException;
    
    public class Main
    {
        public static void main(String[] args)
        {
    	mainMenu();
     	}
    
      //Addition Portion of Program *******//
     //Problem is in this area, not the MainMenu//
    
        public static void addition()
         {
    	    Random generator = new Random();
    		int randomOne = 0;
    		int randomTwo = 0;
    		int product = 0;
    		int userAnswer = 0;
    		boolean done = false;
    
    
    	do {
    	 	try {
    
    		Scanner keyboard = new Scanner(System.in);
    		randomOne = generator.nextInt(10);
    		randomTwo = generator.nextInt(5);
    		product = randomOne + randomTwo;
    		System.out.println("***********ADDITION***********");
    		System.out.println("******************************");
    		System.out.println("*********** " +randomOne+ " + " + randomTwo + "= ***********");
    		System.out.println("******************************");
    		userAnswer = keyboard.nextInt();
    		done = true;
    
    		if (userAnswer == product) {
    
    			    System.out.println("***********CORRECT!***********");
    				System.out.println("******************************");
    				System.out.println();
    		}
    		else{
    				System.out.println("************WRONG!************");
    				System.out.println("******************************");
    				System.out.println();
    		}
    
    		} catch(InputMismatchException e) {
    	 	System.out.println("Sorry! Try again.");
    
    	 	}
    
    	 	} while (!done);
    
    	}
    
    
    
    	 //Main menu portion of program*******//
    	 public static void mainMenu()
    	 {
    	 System.out.println("******CHOOSE A PROBLEM********");
    	 System.out.println("******************************");
    	 System.out.println("******************************");
    	 System.out.println("******** (1) Add      ********");
    	 System.out.println("******** (2) Subtract ********");
    	 System.out.println("******** (3) Multiply ********");
    	 System.out.println("******** (4) Divide   ********");
    	 System.out.println("******** (5) Exit     ********");
    	 System.out.println("********   Choice?    ********");
    	 System.out.println("******************************");
    	 System.out.println();
    	 int x = 1;
    
    	 	  do {
    	 		try {
    
    	 		 System.out.println();
    	 		 Scanner input = new Scanner( System.in );
    	 		 int choice = input.nextInt();
    
    	 		 if(choice == 1){
    	 		 addition();
    	 		 }
    	 		 else if(choice == 5){
    	 		 System.exit(0);
    	 		 }
    	 		 else{
    	         System.out.println("Not an option");}
    			 x=2;
    
    	 		 } catch(Exception e) {
    	 		 System.out.println("Sorry. Try Again");
    	 		 System.out.println();
    	 		 }
    
    	 		} while (x==1);
    
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Your first loop is do while false. So if will fail once thru.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    How is it false? Are you referring to the actual boolean value??

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Yes. Once in the loop you set it to true, so !true == false which is what your while statement contains.
    For a while to continue, it must evaluate to true.

    If you are talking about the other loop, then notice that x = 2 is not inside the else block.

    Perhaps I am not understanding the issue.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    I get you. Im referring to the "Addition" method not the "MainMenu". I set the boolean to false so that if its true then it should run fine. But it's not.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Look at line 37. That is executed before the end of the loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    Ok, so where would i put true evaluation then since i have removed it from line 37

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Under what conditions do you want to exit the loop? Probably when you get the correct answer.
    That is where you would set it to true. However, in your case, since there is no additional processing
    after the while statement, you can just do a hard loop and then use a return to exit the method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    I am trying to set it so that if the user enters any letters then the program just tells them "Sorry, try again" then lets them try again and enter a correct answer. Im basically using this to validate the user input for this portion of the program.

    How would you do a hard loop with a return to exit the method. Can you give me an example?
    Last edited by westcoastrendkill; 11-03-2016 at 10:55 PM.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Here is an example (not tested).

    Java Code:
    public void method() {
       while (true) {
            int ans =  getAnswer();
            if (ans == 50) {
                 System.out.println("Correct!");
                 return;
            }
            System.out.println("Incorrect! Try again!");
        }
    }
    Regards,
    Jim

  11. #11
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    So should I just abandoned the whole try and catch block ??

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    No because you are taking an int from the console. If you type in something that does not parse as
    an int, the program will throw an exception so you need to handle it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    So wait. Im confused. The hard loop that you mentioned would be implemented after the catch right?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    No, when I said a hard loop I meant no changeable conditions in the while statement. Perhaps that was what was confusing.
    Since I may have contributed to some confusion, here is what I meant. Look at the comments. If don't understand it, just ask.

    Regards,
    Jim

    Java Code:
    public static void addition() {
          Random generator = new Random();
          int randomOne = 0;
          int randomTwo = 0;
          int product = 0;
          int userAnswer = 0;
    //      boolean done = false;  // not needed anymore
    
          while (true) { // loop forever
             try {
                Scanner keyboard = new Scanner(System.in);
                randomOne = generator.nextInt(10);
                randomTwo = generator.nextInt(5);
                product = randomOne + randomTwo;
                System.out.println("***********ADDITION***********");
                System.out.println("******************************");
                System.out.println("*********** " + randomOne + " + " + randomTwo
                      + "= ***********");
                System.out.println("******************************");
                userAnswer = keyboard.nextInt();
            //    done = true;  // not needed anymore
    
                if (userAnswer == product) {
                   System.out.println("***********CORRECT!***********");
                   System.out.println("******************************");
                   System.out.println();
                   return;  // leave loop and method
                }
                else {
                   System.out.println("************WRONG!************");
                   System.out.println("******************************");
                   System.out.println();
                }
    
             }
             catch (InputMismatchException e) {
                System.out.println("Sorry! Try again.");
    
             }
    
          }
       }
    }
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Note, I presumed that you wanted to let them try again if they got it wrong. If so, you may want to print
    "Try again!" Otherwise, put a return statement in there too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    Implemented the code and it's still giving the same problem. Not sure what the issue is.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    Look at lines 91 to 93.

    here is what you have, properly formatted.

    Java Code:
    else {
         System.out.println("Not an option");
    }
    x=2;
    Did you meant to put x = 2 inside the else block?
    Can you also explain what you expect when you run the program?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    The program is pretty much a math game. The choices in the mainMenu are the users choice of either addition or subtraction etc. The x=2 variable is used ionly in the mainMenu method. I just use it for my try and catch validation. It holds no sway on the rest of the program

    The Addition portion (part you've been helping me with) works for the most part. The only issue stands, is that the loop isn't working properly. I am only using the try and catch method to validate the user input. That's all. So if the user decides to enter the letters "all" then the program should say "Sorry, Try Again" and then let them try and answer the same math question, not give give em a new one. I've used this method in another program and it works fine, but here it doesn't.

    My teacher is extremely anal about using validation in all sections of the program. Which is why I have been struggling to get this right.
    Last edited by westcoastrendkill; 11-04-2016 at 01:22 AM.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Loop not repeating correctly

    You said that you didn't want to give the user a new problem. But look at your loop. You generate new random numbers each time through.
    You should generate them before you enter the loop (right after you enter the addition method) so the user can try to answer the same math problem. Also, no need to keep creating a scanner instance inside the loop. Just create it once when the method is entered.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Loop not repeating correctly

    It worked!!!! SUCCESS!!! Basically by moving the random numbers out of the loop it stayed the same when i pressed random letters and numbers.

    Thank you for your help Jim. You really helped me out. Thanks

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2013, 05:40 AM
  2. Replies: 1
    Last Post: 02-11-2011, 05:57 AM
  3. jxl repeating rows
    By mermaid in forum New To Java
    Replies: 0
    Last Post: 01-14-2011, 03:59 PM
  4. Repeating
    By AbdulAziz Bader in forum New To Java
    Replies: 3
    Last Post: 05-05-2010, 11:35 PM
  5. Repeating Do-While Statement
    By adamg106 in forum New To Java
    Replies: 10
    Last Post: 04-22-2010, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •