If you know computers check this out..

Collapse

Recommended Videos

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The GIGGAS
    Timbers - Jags - Hokies
    • Mar 2003
    • 28474

    #31
    Re: If you know computers check this out..

    Originally posted by Dtownballa
    heh i wrote that on the way to bed.

    Let me rephrase:

    I want to assign the keypress to activate the switch statement. I'm not sure which line of code that i currently have would acually make that happen, or which line i would need to enter under each case.
    Why not just use an ActionListener that can tell when a button has been pressed to tell if they're ready for calculation to begin?
    Rose City 'Til I Die
    Duuuuuuuvvvvaaaaaaaal
    Hokie Hokie Hokie Hy

    Member: OS Uni Snob Assoc.
    OS OT Post Champ '11

    Twitter: @TheGIGGAS_OS
    Xbox Live: TheGIGGAS
    3DS: 1349-7755-3870

    Comment

    • aukevin
      War Eagle, Go Braves!
      • Dec 2002
      • 14700

      #32
      Re: If you know computers check this out..

      Ok, first off, I was looking at your program again and I see why you had the do whiles around each JOptionPane and I see why you need them (to ask again if the user enters an invalid number). So I'm sorry if I steered you wrong on that. That works good

      Ok, now for the help you actually asked for

      I don't think you need any kind of ActionListener to activate the switch statement. The way you have your code set up, after you receive the number that maps to which math operator you use (after the do while loop that the JOptionPane is within to verify that the number is between 1 and 4), this is where you do the switch statement. The switch statement will calculate your answer and store it in your results double, then you display that answer in another JOptionPane (just a simple informational pop-up like you used to tell the user that an input was incorrect). After that, ask the user if they won't to go again, and either go again or exit.
      Last edited by aukevin; 10-16-2007, 09:34 AM.

      Atlanta Braves
      - Auburn Tigers - Nashville Predators

      Comment

      • Dtownballa
        MVP
        • Feb 2003
        • 1450

        #33
        Re: If you know computers check this out..

        here is where I am so far.
        Attached Files
        PSN: The_TechNinja
        Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

        Comment

        • Dtownballa
          MVP
          • Feb 2003
          • 1450

          #34
          Re: If you know computers check this out..

          Originally posted by The GIGGAS
          Why not just use an ActionListener that can tell when a button has been pressed to tell if they're ready for calculation to begin?

          Never learned it.

          we suppose to utilize the methods we learned.
          PSN: The_TechNinja
          Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

          Comment

          • aukevin
            War Eagle, Go Braves!
            • Dec 2002
            • 14700

            #35
            Re: If you know computers check this out..

            Originally posted by Dtownballa
            here is where I am so far.
            Still looks like you are on the right track. (HINT: You might want to check your variables in your if statement when verifying the second number entered.) You also still need to declare the results double at the top and you are missing some closing curly braces at the end of the program.

            I don't see where you are going with the operation String, I don't see why you need that and what you gain in the switch statement by trying to assign it to something. You have that results value, just display it in a JOptionPane after you close the switch statement.

            Your switch statement syntax looks good now

            Atlanta Braves
            - Auburn Tigers - Nashville Predators

            Comment

            • Dtownballa
              MVP
              • Feb 2003
              • 1450

              #36
              Re: If you know computers check this out..

              Kevin,
              Thanks for all of your help, without you I dont know where I'll be in this program, PM your pay pal info, and i'll give you donation for a cup of coffee or something. This is major headace for me.

              Now, the reason im putting the operation string in there is because, I'm not sure how to link the cases to the mathmaticall operation. This is where i'm stuck at.

              Also, I'm not sure if i have my JOption pane to display the results is in the correct spot or even the correct format.

              Because im my program it's only going to inset the second number, I seem to cannot get any further.
              PSN: The_TechNinja
              Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

              Comment

              • aukevin
                War Eagle, Go Braves!
                • Dec 2002
                • 14700

                #37
                Re: If you know computers check this out..

                LOL, don't worry about the paypal. I used to bug my professors/teaching assistants up the wall with programming questions when I first started out.

                Now, the reason im putting the operation string in there is because, I'm not sure how to link the cases to the mathmaticall operation. This is where i'm stuck at.
                You are already doing this with the switch statement you have. You already have a JOptionPane that asks the user for a math operator:

                Originally posted by from your code
                s = JOptionPane.showInputDialog("Select a math operation: \n 1: Add \n 2: Subtract \n 3: Multiply \n Divide");
                choice = Integer.parseInt(s);
                and your switch statement is calculating the results from that choice:

                Originally posted by from your code again
                switch (choice)
                {
                case 1:
                {
                results = (num1 + num2);
                operation = addition;

                break;
                }


                case 2:
                {
                results = (num1 - num2);
                operation = subtraction;
                break;
                }


                case 3:
                {
                results = (num1 * num2);
                operation = multiplication;
                break;
                }

                case 4:
                {
                results = (num1 / num2);
                operation = division;
                break;
                }

                {
                That last curly brace closing the switch is the wrong direction by the way (that's why I thought you might be missing some at the end I think). Case 1 in the switch statement relates to the choice variable having a 1 in it. Case 2 in the switch statement relates to the choice variable having a 2 in it. That's your link to the math operator right there.

                The way your program is right now all you have to do (except for fixing missing or wrong direction curly braces and some other minor problems) is display the results variable in a new JOptionPane. And then loop it again to run the program again by possibly using the method I hinted at last week with a stop variable.
                Last edited by aukevin; 10-16-2007, 10:56 AM.

                Atlanta Braves
                - Auburn Tigers - Nashville Predators

                Comment

                • Dtownballa
                  MVP
                  • Feb 2003
                  • 1450

                  #38
                  Re: If you know computers check this out..

                  I'm seeing what your saying. Now my question is, I am unsure where should i put my JOptionpane at. before the switch or after?

                  Because as of right now after i enter my second number the option does not come up. I think the coding is correct, now i just need to to display up. Ive tried moving it around, with trial and error but I am seeing the same results
                  PSN: The_TechNinja
                  Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

                  Comment

                  • aukevin
                    War Eagle, Go Braves!
                    • Dec 2002
                    • 14700

                    #39
                    Re: If you know computers check this out..

                    Originally posted by Dtownballa
                    I'm seeing what your saying. Now my question is, I am unsure where should i put my JOptionpane at. before the switch or after?

                    Because as of right now after i enter my second number the option does not come up. I think the coding is correct, now i just need to to display up. Ive tried moving it around, with trial and error but I am seeing the same results
                    From looking at the last code you posted, your first two JOptionPanes are correct, except for the condition in the if statement for verifying the second number.

                    Your JOptionPane that asks for the math operator must go before the switch statement because it fills in the choice variable which is needed for the switch statement. That's how you have it in the code you posted, so that's good. You need to use the same do while loop technique you used for the first two JOptionPanes around the JOptionPane that asks for the math operator just in case the user doesn't enter 1-4. You need to check that condition just like you did the 1-35 condition for the two numbers that are first inputted.

                    After you get that taken care of, your switch statement is fine except the closing curly brace for the switch that is facing the wrong way (and take out that operation equals stuff because I don't think you need it (comment it out if you don't want to delete it yet).

                    However, I don't get how your code is compiling because you haven't declared your results variable or those variables you are trying to set equal to the operation String (which I don't think you need anyway). You have some curly brace issues too. So I'm not sure how you are even able to get the first two pop-ups to show up when it isn't compiling.

                    Anyway, once you fix your syntax problems (curly brace issues) or not declaring variables (like the results one), I would put a System.out.println(results) in the code right after you close the switch instead of the System.exit(0) so that you can print out and see the right computation. After you see that you can print out the right value, you can put that results value on a JOptionPane.

                    Atlanta Braves
                    - Auburn Tigers - Nashville Predators

                    Comment

                    • Dtownballa
                      MVP
                      • Feb 2003
                      • 1450

                      #40
                      Re: If you know computers check this out..

                      Declearing results as a varible is as simple as:

                      // Declare Variables

                      double num1;
                      double num2;
                      int choice;
                      String s;
                      results;


                      but i get an error stating that results is not a statement.
                      PSN: The_TechNinja
                      Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

                      Comment

                      • aukevin
                        War Eagle, Go Braves!
                        • Dec 2002
                        • 14700

                        #41
                        Re: If you know computers check this out..

                        Originally posted by Dtownballa
                        Declearing results as a varible is as simple as:

                        // Declare Variables

                        double num1;
                        double num2;
                        int choice;
                        String s;
                        results;


                        but i get an error stating that results is not a statement.
                        you gotta say what type results is:

                        double results = 0;

                        Atlanta Braves
                        - Auburn Tigers - Nashville Predators

                        Comment

                        • Dtownballa
                          MVP
                          • Feb 2003
                          • 1450

                          #42
                          Re: If you know computers check this out..

                          double results?

                          or INt results?

                          I've tried both, and still I cannot get past the 2nd phrase of numbers.

                          here is where i'm at.
                          Attached Files
                          PSN: The_TechNinja
                          Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

                          Comment

                          • aukevin
                            War Eagle, Go Braves!
                            • Dec 2002
                            • 14700

                            #43
                            Re: If you know computers check this out..

                            Originally posted by Dtownballa
                            double results?

                            or INt results?

                            I've tried both, and still I cannot get past the 2nd phrase of numbers.

                            here is where i'm at.
                            It needs to be a double because it could possibly store the results of a division. You need to initialize it to zero also, otherwise you will get a compile error because it would never have a value assigned to it before it is printed out at the end in the case that the results variable never was assigned a value in the switch statement.

                            You are also missing a closed curly brace at the end. After that, it should compile.

                            Atlanta Braves
                            - Auburn Tigers - Nashville Predators

                            Comment

                            • Dtownballa
                              MVP
                              • Feb 2003
                              • 1450

                              #44
                              Re: If you know computers check this out..

                              Originally posted by aukevin
                              It needs to be a double because it could possibly store the results of a division. You need to initialize it to zero also, otherwise you will get a compile error because it would never have a value assigned to it before it is printed out at the end in the case that the results variable never was assigned a value in the switch statement.

                              You are also missing a closed curly brace at the end. After that, it should compile.
                              It works great! i acually yelled out loud when i saw it in action, my co workers looked at me like I was a mad man. haha.

                              One more request my good man, how do i JOption pane the results?
                              PSN: The_TechNinja
                              Check me out on YouTube as well: http://tinyurl.com/lqd6s9j

                              Comment

                              • aukevin
                                War Eagle, Go Braves!
                                • Dec 2002
                                • 14700

                                #45
                                Re: If you know computers check this out..

                                You would use a JOptionPane just like you did when you were showing an error for when the user entered a bad value for the numbers.

                                Originally posted by from your code
                                JOptionPane.showMessageDialog(null, "Please enter a number between 1 and 35.", "A Wrong Number Was Entered", JOptionPane.ERROR_MESSAGE);
                                Except you would change the message and title in it to make sense for displaying the results. Do you know how to append a variable to a string? The message that appears in the JOptionPane is just a String, so you would create a String that displays the result.

                                For example if you have these two statements:
                                String name = "Kevin";
                                String s1 = “My name is” + name;


                                If you said: System.out.println(s1);
                                You would get: "My name is Kevin"

                                So if I wanted a JOptionPane that showed my string above, it would be:

                                JOptionPane.showMessageDialog(null, "My name is "+name, "This is how you append values to a String", JOptionPane.INFORMATION_MESSAGE);

                                So the you need to make a string that displays the results variable just like how above shows the name variable.

                                Atlanta Braves
                                - Auburn Tigers - Nashville Predators

                                Comment

                                Working...