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

Thread: Looping through a while loop with a switch statement

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looping through a while loop with a switch statement

    Hello all. I am new to Java and I am have some issues with some code I'm working on. I am having a problem with looping a while loop that contains a switch statement. Here is my code:

    package hw4jenningsd;

    import javax.swing.*;

    public class HW4JenningsD {

    public static class mathOps {
    private int numOne;
    private int numTwo;
    private int numThreeOne;
    private int numThreeTwo;
    private String inputStringOne;
    private String inputStringTwo;

    public mathOps() {
    numOne = 0;
    numTwo = 0;
    numThreeOne = 0;
    numThreeTwo = 0;
    } //end constructor

    public void input() {
    inputStringOne = JOptionPane.showInputDialog(null,"Enter your first "
    + "integer number");
    numOne = Integer.parseInt(inputStringOne);
    inputStringTwo = JOptionPane.showInputDialog(null,"Enter you second "
    + "integer number");
    numTwo = Integer.parseInt(inputStringTwo);
    }// end input

    public void addition() {
    numThreeOne = numOne + numTwo;
    numThreeTwo = numOne + numTwo;
    }// end addtion

    public void subtraction() {
    numThreeOne = numOne - numTwo;
    numThreeTwo = numTwo - numOne;
    }// end subtraction

    public void multiplication() {
    numThreeOne = numOne * numTwo;
    numThreeTwo = numTwo * numOne;
    }// end multiplication

    public void division() {
    numThreeOne = numOne / numTwo;
    numThreeTwo = numTwo / numOne;
    }// end division

    public void modulo() {
    numThreeOne = numOne % numTwo;
    numThreeTwo = numTwo % numOne;
    }// end modulo

    public void displayResult() {
    JOptionPane.showMessageDialog(null, "The result of your "
    + "calculations are " + numThreeOne + " and " +numThreeTwo+"\n and the two integers "
    + "you entered were "+numOne+ " and " +numTwo+ "\n and the type "
    + "of operation performed was "); //another problem. How can I code into this line the type of math operation was performed?
    }// end displayResult

    }// end class mathOps

    public static void main(String[] args) {
    String operationString;
    int repeatOperation2;
    int operationChoice;
    mathOps mathOpsOne = new mathOps();
    mathOpsOne.input();

    JOptionPane.showMessageDialog(null,"Math operations Menu\nThe "
    + "purpose of this menu is to allow the user "
    + "to choose a\nmath operation to be performed.\n\n"
    + "1. +\n2. -\n3. *\n4. /\n5. %\n6. End the program.");
    repeatOperation2 = 1;
    operationString = JOptionPane.showInputDialog(null,"Press 1 to add,"
    + " 2 to subtract, 3 to multiply, 4 to divide, 5 for modulo, "
    + "or 6 to end the program");
    operationChoice = Integer.parseInt(operationString);

    while(repeatOperation2 == 1) {
    switch(operationChoice) {
    case 1: mathOpsOne.addition();
    break;
    case 2: mathOpsOne.subtraction();
    break;
    case 3: mathOpsOne.multiplication();
    break;
    case 4: mathOpsOne.division();
    break;
    case 5: mathOpsOne.modulo();
    break;
    case 6: JOptionPane.showMessageDialog(null,"Thanks for using "
    + "my program.");System.exit(0);
    break;
    default:JOptionPane.showInputDialog(null,"The selection is not"
    + " acceptable.\n Enter 0 to try again.");
    break;
    }// end switch operationChoice

    mathOpsOne.displayResult();repeatOperation2 = 2;

    while(repeatOperation2 == 2){
    repeatOperation2 = Integer.parseInt(JOptionPane
    .showInputDialog("Do you want to use the same "
    + "integers or a new set of integers" +
    "\nPress [0] for new integers or [1] for same integers."));// one more problem. If the user chooses the same integers, how can I code it so that the user does not enter a new set of integers?
    if(repeatOperation2 == 0) //this is where my problem is. I want the program to loop back to the math menu.
    mathOpsOne.input();
    else if(repeatOperation2 == 1){
    repeatOperation2 = 1;
    }
    }

    }// end while
    JOptionPane.showMessageDialog(null,"Thanks for using "
    + "my program. ");System.exit(0);
    }// end main

    }// end program


    Thanks for any assistance.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Looping through a while loop with a switch statement

    Hi, please format your code with code tags (see my sig link).

    What kind of problem do you have? Compiler or runtime errors? Unexpected output? etc.?

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

    Default Re: Looping through a while loop with a switch statement

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link. Most won't bother to help someone who has incorrectly posted unformatted code. We also won't spend time figuring out what "some issues" means. Explain what you need help with as explicitly as you can.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default Re: Looping through a while loop with a switch statement

    if you are trying to make it a menu-driven program(where user has to select from menu) then it is better to use do...while loop ,instead of while..!!! do-while will make the user to use your program in an effective manner .

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping through a while loop with a switch statement

    package math;
     
    import javax.swing.*;
     
    public class math {
     
    public static class mathOps {
        private int numOne;
        private int numTwo;
        private int numThreeOne;
        private int numThreeTwo;
        private String inputStringOne;
        private String inputStringTwo;             
     
        public mathOps() {
            numOne = 0;
            numTwo = 0;
            numThreeOne = 0;
            numThreeTwo = 0;
        } //end constructor
     
        public void input() {
            inputStringOne = JOptionPane.showInputDialog(null,"Enter your first "
                + "integer number");
            numOne = Integer.parseInt(inputStringOne);
            inputStringTwo = JOptionPane.showInputDialog(null,"Enter you second "
                + "integer number");
            numTwo = Integer.parseInt(inputStringTwo);                
        }// end input
     
        public void addition() {
            numThreeOne = numOne + numTwo;     
            numThreeTwo = numOne + numTwo; 
        }// end addtion
     
        public void subtraction() {
            numThreeOne = numOne - numTwo;
            numThreeTwo = numTwo - numOne; 
        }// end subtraction
     
        public void multiplication() {
            numThreeOne = numOne * numTwo;
            numThreeTwo = numTwo * numOne;  
        }// end multiplication
     
        public void division() {
            numThreeOne = numOne / numTwo;
            numThreeTwo = numTwo / numOne;         
        }// end division
     
        public void modulo() {
            numThreeOne = numOne % numTwo;
            numThreeTwo = numTwo % numOne; 
        }// end modulo
     
        public void displayResult() {
            JOptionPane.showMessageDialog(null, "The result of your "
            + "calculations are " + numThreeOne + " and " +numThreeTwo+"\n and the two integers "
            + "you entered were "+numOne+ " and " +numTwo+ "\n and the type "
            + "of operation performed was "); //<ERROR 1
        }// end displayResult  
     
    }// end class mathOps 
     
        public static void main(String[] args) {
            String operationString;
            int repeatOperation2;
            int operationChoice;
            mathOps mathOpsOne = new mathOps();       
            mathOpsOne.input();         
     
            JOptionPane.showMessageDialog(null,"Math operations Menu\nThe "
                    + "purpose of this menu is to allow the user "
                    + "to choose a\nmath operation to be performed.\n\n"
                    + "1. +\n2. -\n3. *\n4. /\n5. %\n6. End the program.");
                    repeatOperation2 = 1;         
            operationString = JOptionPane.showInputDialog(null,"Press 1 to add,"
                    + " 2 to subtract, 3 to multiply, 4 to divide, 5 for modulo, "
                    + "or 6 to end the program");  
            operationChoice = Integer.parseInt(operationString);                
     
            while(repeatOperation2 == 1) {             
                switch(operationChoice) {                
                    case 1: mathOpsOne.addition();
                        break;                    
                    case 2: mathOpsOne.subtraction();
                        break;                    
                    case 3: mathOpsOne.multiplication();
                        break;                    
                    case 4: mathOpsOne.division();
                        break;                    
                    case 5: mathOpsOne.modulo();
                        break;                    
                    case 6: JOptionPane.showMessageDialog(null,"Thanks for using "
                        + "my program.");System.exit(0); 
                        break;                  
                    default:JOptionPane.showInputDialog(null,"The selection is not"
                        + " acceptable.\n Enter 0 to try again.");  //<ERROR 2
                        break;
                }// end switch operationChoice
     
            mathOpsOne.displayResult();repeatOperation2 = 2;
     
                while(repeatOperation2 == 2){
                    repeatOperation2 = Integer.parseInt(JOptionPane
                    .showInputDialog("Do you want to use the same "
                     + "integers or a new set of integers" +
                    "\nPress [0] for new integers or [1] for same integers.")); //ERROR 3
                if(repeatOperation2 == 0)
                    mathOpsOne.input();        
                else if(repeatOperation2 == 1){
                    repeatOperation2 = 1;                
                    }
                }   
     
            }// end while
        JOptionPane.showMessageDialog(null,"Thanks for using "
                + "my program.");System.exit(0);         
        }// end main
     
    }// end program

    Error 1. After the user has selected a math operation, it should output a display on what math function has been performed (add, subtract, etc. ).

    Error 2. If the user selects a value other than 1 - 6 in the switch, an error message displays notifying the user to press 0 to try again. Instead of going back to the select a math operation pane, the display result pane pops up.

    Error 3. After the display result, the user has the option of entering new integers or using the same integers to perform another math operation. If the user selects same, the option to enter new integers should not appear, just the option to choose a different math function. My code just displays the results from the first set of integers.

    The code compiles with no errors, I am just trying to get it to operate as it should. Also, I added the tags to the code but I am not sure as to why it is not color-coded.

    I hope this is a better explanation of my problems and I will do better on my forum etiquette. Thanks again all.
    Last edited by madgame618; September 24th, 2014 at 09:52 AM. Reason: Better Explanation

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping through a while loop with a switch statement

    Can you copy the contents of the command prompt window from when you test the program and paste it here so we can see what the program is doing. Add some comments where the output is not what you want describing what you want the output to look like.

    Add some println() statements next to all the JOptionPane method calls to show what was displayed and what was returned. That will give you something to copy here to show what happens when the code is tested.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Looping through a while loop with a switch statement

    Thank you for cleaning up your post and describing the help you need so clearly.

    By Java's naming conventions, class names should begin with capital letters.

    Is using a static nested class your idea, or was it mandated by the assignment?

    Have you written a similar program with input/output done through the console? If so, I suggest you model the design of this program after that one. If you haven't written a similar console program, then . . . you should have.

    For your questions:

    Error 1: Set a String that contains the operation chosen and print it after the " was ". You could set the operation String in the corresponding function method.

    Error 2: Use an if() statement to branch the program to the desired operation.

    Error 3: Use an if() statement and/or a boolean flag to set the state of the program to repeat or accept new operands.

    Note: The highlight tags, [highlight=java]code here[/highlight], do the highlighting while the code tags simply preserve formatting.

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. for-each loop and a switch statement
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2013, 02:02 PM
  4. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  5. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM