Need help with Split String to create a calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Max347
    New Member
    • Feb 2008
    • 10

    Need help with Split String to create a calculator

    This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers and an operation, with and output of the answer with the original equation. I think I need to use the split function, however I dont know what to do once it is split. Here is what I have so far-

    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    public class Calculator{
    public static void main (String[] args){
    String inputString = JOptionPane.sho wInputDialog("b lah blah");
    String[] result = inputString.spl it("//s");
    for (int x=0; x<result.length ; x++)
    }
    }

    I'm new, if you can't tell, so im sorry if this is extremely elementary. In my mind, I would want to give each part of the string a name or letter, then use those for the rest of the code. I dont know if this is possible. Ex- input- "2 + 1" would give a=2 b=+ c=1, then I could go about using if statements to separate the operations (I need +-/* and % (remainder)). Thanks! Max
  • Max347
    New Member
    • Feb 2008
    • 10

    #2
    Also, ive found references to arrays, so maybe thats my solution? If someone has a guide?
    Thanks

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Do it in small steps and satisfy yourself that the current step is correctly done before moving to the next step.
      1.) Make sure you are getting the input correctly from the user. System.out.prin t it out to the console to test this.
      2.) Make sure your splitting is working as expected. Print out each value in the array to test this.(Can use java.util.Array s.toString for this).
      3.) Now you can proceed to do the calculation depending on what the second value in your array is.

      Comment

      • Max347
        New Member
        • Feb 2008
        • 10

        #4
        Originally posted by r035198x
        Do it in small steps and satisfy yourself that the current step is correctly done before moving to the next step.
        1.) Make sure you are getting the input correctly from the user. System.out.prin t it out to the console to test this.
        2.) Make sure your splitting is working as expected. Print out each value in the array to test this.(Can use java.util.Array s.toString for this).
        3.) Now you can proceed to do the calculation depending on what the second value in your array is.
        My requirements limit me to the one output, would this be easier to tokenize, and make tokens? Thanks!

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Max347
          My requirements limit me to the one output..
          What do you mean? Surely you can use System.out.prin tln statements during your testing. You canthen remove them when you submit the final thing to whoever.

          Originally posted by Max347
          .., would this be easier to tokenize, and make tokens? Thanks!
          That's what String.split is doing in your code. It's breaking the input into tokens.

          Comment

          • Max347
            New Member
            • Feb 2008
            • 10

            #6
            Oh alright, I gotcha on the outputs. As for the splits, how do I actually use them? Will the result change throughout the code as I use it for different things? What im asking, is can I name them or something? Thanks

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Max347
              Oh alright, I gotcha on the outputs. As for the splits, how do I actually use them? Will the result change throughout the code as I use it for different things? What im asking, is can I name them or something? Thanks
              result is a String array. You can access its elements using
              result[0] //first element
              result[i]// (i+1)th element

              Comment

              • Max347
                New Member
                • Feb 2008
                • 10

                #8
                Originally posted by r035198x
                result is a String array. You can access its elements using
                result[0] //first element
                result[i]// (i+1)th element
                Alright cool, thats exactly what I was looking for.
                Ok, I put in the String a = a, etc, however it says it is not a statement.

                String a = result[0];
                String b = result[1];
                String c = result[2];
                switch (b)
                {
                case +:
                case -:
                case /:
                case *:

                I know I need to fill in each possible case, but is it right? The complier is telling me I can't do the String a = result[x]; lines. Also, do I need anything around each operation? Thanks

                Im sorry in advance is this stuff is really stupid, its my second week in the class and the book is unclear. Thanks!

                Comment

                • Max347
                  New Member
                  • Feb 2008
                  • 10

                  #9
                  Ok, ive been experimenting, and took out the whole String a, String b business, I guess they dont need to specifically be defined. I now still have the "illegal start of expression on the colons in my cases. Am I missing something? Thanks

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Max347
                    Ok, ive been experimenting, and took out the whole String a, String b business, I guess they dont need to specifically be defined. I now still have the "illegal start of expression on the colons in my cases. Am I missing something? Thanks
                    You can't switch on Strings.
                    If you want to use a switch then you need to change the result[1] value to a char(you can switch on chars). Otherwise use if-else statements.

                    P.S Use Sun's Java tutorial as your reference. You can even download the whole thing to your computer.

                    Comment

                    • Max347
                      New Member
                      • Feb 2008
                      • 10

                      #11
                      Originally posted by r035198x
                      You can't switch on Strings.
                      If you want to use a switch then you need to change the result[1] value to a char(you can switch on chars). Otherwise use if-else statements.

                      P.S Use Sun's Java tutorial as your reference. You can even download the whole thing to your computer.
                      What about floats? how about this-
                      This is what ive got-

                      Float.parseFloa t(result[0]) = n1;
                      Float.parseFloa t(result[2]) = n2;
                      switch (1)
                      {
                      case '+':
                      JOptionPane.sho wMessageDialog( inputString + " = " + n1 + n2);


                      Will that output 2 + 1 = 3? (I cant run it yet) Thanks

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Max347
                        What about floats? how about this-
                        This is what ive got-

                        Float.parseFloa t(result[0]) = n1;
                        Float.parseFloa t(result[2]) = n2;
                        switch (1)
                        {
                        case '+':
                        JOptionPane.sho wMessageDialog( inputString + " = " + n1 + n2);


                        Will that output 2 + 1 = 3? (I cant run it yet) Thanks
                        You really do need to go through that tutorial.
                        You probably wanted to do
                        [CODE=java]float n1 = Float.parseFloa t(result[0]);
                        float n2 = Float.parseFloa t(result[2]);
                        char c = result[1].chatAt(0);
                        switch(c) {
                        ....[/CODE]

                        Comment

                        • Max347
                          New Member
                          • Feb 2008
                          • 10

                          #13
                          Yeah, I know I do. Im downloading it right now (thanks for the link btw). Here's what I have-

                          import javax.swing.*;
                          import java.awt.*;
                          import java.util.*;
                          public class Calculator{
                          public static void main (String[] args) {
                          String inputString = JOptionPane.sho wInputDialog("I nput...etc etc");
                          String[] result = inputString.spl it("//s");
                          for (int x=0; x<result.length ; x++)
                          float n1 = Float.parseFloa t(result[0]);
                          float n2 = Float.parseFloa t(result[2]);
                          char c = result [1].chatAt(0);
                          switch (c)
                          {
                          case '+':
                          JOptionPane.sho wMessageDialog( inputString + " = " + n1 + n2);
                          break;
                          case '-':
                          JOptionPane.sho wMessageDialog( inputString + " = " + n1 - n2);
                          break;
                          case '/':
                          JOptionPane.sho wMessageDialog( inputString + " = " + n1 / n2);
                          break;
                          case '*':
                          JOptionPane.sho wMessageDialog( inputString + " = " + n1 * n2);
                          break;
                          case '%':
                          JOptionPane.sho wMessageDialog( inputString + " = " + n1 % n2);
                          break;
                          }
                          }
                          }

                          I really appreciate you taking the time to do this for me, a stranger. thanks, Max

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            1.) Use code tags when posting code (like I did above).
                            2.) Don't just copy the code, try to understand it. (You probably didn't pick that I made a typo in charAt).
                            3.) What is the for loop for?
                            4.) There is no showMessageDial og method in JOptionpane which takes one string as argument.

                            Comment

                            • Max347
                              New Member
                              • Feb 2008
                              • 10

                              #15
                              Originally posted by r035198x
                              1.) Use code tags when posting code (like I did above).
                              2.) Don't just copy the code, try to understand it. (You probably didn't pick that I made a typo in charAt).
                              3.) What is the for loop for?
                              4.) There is no showMessageDial og method in JOptionpane which takes one string as argument.
                              Ok, sorry about the code. I took out the loop. What if I just put one output on the bottom? null? When I compile this, it goes through now, asks me for my info, but then doesnt output. Here is what I have-
                              Code:
                              import javax.swing.*;
                              import java.awt.*;
                              import java.util.*;
                              public class Calculator{
                              public static void main (String[] args) {
                              String inputString = JOptionPane.showInputDialog("Input...etc etc");
                              String[] result = inputString.split("//s");
                              
                              float n1 = Float.parseFloat(result[0]);
                              float n2 = Float.parseFloat(result[2]);
                              char c = result [1].chatAt(0);
                              switch (c)
                              {
                              case '+':
                              JOptionPane.showMessageDialog(null, inputString + " = " + n1 + n2);
                              break;
                              case '-':
                              JOptionPane.showMessageDialog(null, inputString + " = " + n1 - n2);
                              break;
                              case '/':
                              JOptionPane.showMessageDialog(null, inputString + " = " + n1 / n2);
                              break;
                              case '*':
                              JOptionPane.showMessageDialog(null, inputString + " = " + n1 * n2);
                              break;
                              case '%':
                              JOptionPane.showMessageDialog(null, inputString + " = " + n1 % n2);
                              break;
                              }
                              }
                              }

                              Comment

                              Working...