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 11 of 11

Thread: Java Noob - Terminating loop.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Noob - Terminating loop.

    Hi, I am currently building a Menu driven calculator, and i have got my loop to go, but now when the user enters " 6 " from the menu - the program does not terminate. How can i get this to end?

    I also tried using a " Break;" but it didnt seem to end anything

    import java.util.Scanner;
     
    public class MenuDrivenCalculator {
    	// Main menu
    	public static void main(String[] args) {
    		// Create Scanner
    		Scanner input = new Scanner(System.in);
    		int invalid = 0;
    		while (invalid <= 3) {
    			System.out.println(" Menu: ");
    			System.out.println("1. Add ");
    			System.out.println("2. Sub ");
    			System.out.println("3. Multiply ");
    			System.out.println("4. Divide ");
    			System.out.println("5. Generate a Random Number");
    			System.out.println("6. Quit");
    			// Complete the input on what you would like to do.
    			System.out.println(" What would you like to do?");
    			int menuNumber = input.nextInt();
     
    			if (menuNumber < 1) {
    				System.out.println("Im Sorry " + menuNumber
    						+ " is not an option on the menu");
    				invalid = invalid + 1;
    			}
    			if (menuNumber > 6) {
    				System.out.println("Im Sorry " + menuNumber
    						+ " is not an option on the menu");
    				invalid = invalid + 1;
    			}
     
    			if (menuNumber == 1) {
    				System.out.println("What is the first Number?:");
    				int number1 = input.nextInt();
    				System.out.println("What is the Second Number?:");
    				int number2 = input.nextInt();
    				int answer = number1 + number2;
    				System.out.println("Answer : " + answer);
    				invalid = 0;
    			}
    			if (menuNumber == 2) {
    				System.out.println("What is the first Number?:");
    				int number1 = input.nextInt();
    				System.out.println("What is the Second Number?:");
    				int number2 = input.nextInt();
    				int answer = number1 - number2;
    				System.out.println("Answer : " + answer);
    				invalid = 0;
    			}
    			if (menuNumber == 3) {
    				System.out.println("What is the first Number?:");
    				int number1 = input.nextInt();
    				System.out.println("What is the Second Number?:");
    				int number2 = input.nextInt();
    				int answer = number1 * number2;
    				System.out.println("Answer : " + answer);
    				invalid = 0;
     
    			}
    			if (menuNumber == 4) {
    				System.out.println("What is the first Number?:");
    				int number1 = input.nextInt();
    				System.out.println("What is the Second Number?:");
    				int number2 = input.nextInt();
     
    				if (number2 != 0) {
    					System.out.println(number1 / number2);
    					// Divides first number by second number
    				} else if (number2 == 0) {
    					System.out.println("I'm sorry, you cannot divide by zero.");
    					invalid = 0;
    				}
    				if (menuNumber == 5) {
    					System.out.println("What is the lower limit?:");
    					int min = input.nextInt();
    					System.out.println("What is the upper limit?:");
    					int max = input.nextInt();
     
    					int answer = (int) (Math.ceil(Math.random() * (max - min)) + min);
     
    					System.out.println("Answer: " + answer);
    					invalid = 0;
    				}
     
    				if (menuNumber == 6) {
    					System.exit(0); 
    				}
     
    			}
     
    		}
    	}
    }
    Last edited by Famous112; September 7th, 2014 at 05:15 PM. Reason: Changed question.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java Noob - Using for Loop to terminate the program after 3 incorrect inputs - in a row. // How to continue loop after Arithmetic error.

    You could either check whether the divisor is 0 before doing the division and then print the error statement (and, of course, not do the division), or put the division in a try-catch block catching the arithmetic exception.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 689 Times in 676 Posts

    Default Re: Java Noob - Using for Loop to terminate the program after 3 incorrect inputs - in a row. // How to continue loop after Arithmetic error.

    A for (; ; ) loop is an atypical approach to an infinite loop. A while ( true ) loop is more conventional. I'm not a fan of infinite loops, but I suppose they have their place.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Noob - Using for Loop to terminate the program after 3 incorrect inputs - in a row. // How to continue loop after Arithmetic error.

    Would a while (true) Loop work if i wanted to terminate the program after 3 incorrect inputs? I'm trying to figure out how i could do that.

    Ill look at the Try-catch block. this is all still so new to me, but im working on it.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 689 Times in 676 Posts

    Default Re: Java Noob - Using for Loop to terminate the program after 3 incorrect inputs - in a row. // How to continue loop after Arithmetic error.

    Investigate the break statement while you're in the docs.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Noob - Terminating loop.

    Changed the question! Now i need to figure out how to allow it to terminate? it just keeps looping.. D:

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java Noob - Terminating loop.

    As far as I can see you already implemented that.

  8. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Noob - Terminating loop.

    Quote Originally Posted by Cornix View Post
    As far as I can see you already implemented that.
    I did, But when it runs, it wont end if the user enters " 6 " from what i implemented to exit the program.

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java Noob - Terminating loop.

    Of course not, look at the indention and your curly brackets. They are incorrectly placed, that is why the input 5 and 6 will always be ignored.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 689 Times in 676 Posts

    Default Re: Java Noob - Terminating loop.

    There is a long string of if() statements. For a menu item selection, if/else statements may be more appropriate, if for no other reason than to keep things straight. For example, in your code, is the if() statement that checks for 6 at the same level as the rest, or is it buried in another condition?

  11. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Noob - Terminating loop.

    Quote Originally Posted by Cornix View Post
    Of course not, look at the indention and your curly brackets. They are incorrectly placed, that is why the input 5 and 6 will always be ignored.
    They are incorrectly placed? This is so confusing D:

    --- Update ---

    Figured it out, Thank you guys !!!!

Similar Threads

  1. Why won't my program compile? (While loop with inputs)
    By LWorm in forum Loops & Control Statements
    Replies: 5
    Last Post: October 14th, 2013, 10:52 PM
  2. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 02:40 AM
  3. java Loop program to place distinct value in a row, column and diagonal
    By javanovoice in forum Loops & Control Statements
    Replies: 3
    Last Post: September 8th, 2011, 08:08 PM
  4. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  5. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM