Very Simple Calculator Program Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NoviceJava
    New Member
    • Mar 2008
    • 14

    Very Simple Calculator Program Help

    I'm new to JAVA and need some help writing a simple calculator program.

    Here are the instructions:

    -expects espression with 2 operands together with either +, -, *, or / operator
    -espects only positive input
    -assumes all real #'s
    -assumes operands and operators in expression are separated with spaces

    here is what I have so far:

    import java.io.*;
    import java.util.*;
    public class Calculator
    {
    public static void main(String[] args)
    throws java.io.IOExcep tion
    {
    String inputString, s1, s2, s3;
    float num1, num2, num3, result;

    InputStreamRead er isr = new InputStreamRead er(System.in);

    BufferedReader br = new BufferedReader( isr);

    System.out.prin t("What do you want to compute? ");
    inputString = br.readLine();

    StringTokenizer st = new StringTokenizer (inputString);

    s1 = st.nextToken();
    s2 = st.nextToken();
    s3 = st.nextToken();

    num1 = Float.parseFloa t(s1);
    num2 = Float.parseFloa t(s3);

    if (s2.equals("+") )
    result = (num1 + num2);
    else if (s2.equals("-"))
    result = (num1 - num2);
    else if (s2.equals("/"))
    result = (num1 / num2);
    else if (s2.equals("*") )
    result = (num1 * num2);

    System.out.prin tln("The result is " + result);
    }
    }
  • Stwange
    Recognized Expert New Member
    • Aug 2007
    • 126

    #2
    What have you tried so far, what is it doing and what were you expecting it to do? You can't just post the assignment here along with the contributed code and expect us to fill the blanks in for you.
    Make an effort, and I'll be happy to help you if you go wrong.

    Comment

    • NoviceJava
      New Member
      • Mar 2008
      • 14

      #3
      Originally posted by Stwange
      What have you tried so far, what is it doing and what were you expecting it to do? You can't just post the assignment here along with the contributed code and expect us to fill the blanks in for you.
      Make an effort, and I'll be happy to help you if you go wrong.
      I'm not sure what you mean. The code above is what I have worked on so far. When I try to compile it I get an error message saying that the variable result may not have been initialized.

      I am expecting it to read "What do you want to compute," whereby I need to input the expression in the required format mentioned previously and for it to give me the result after reading "The result is." The expression only needs to involve two operands. I've tried many different things and I think what I have now is the best of what I have tried thus far; I read a similiar post and it mentioned that the tokenizer should be used so I incorporated it into the source code.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Your compiler is complaining about result because you only assign it values in your if statements. As such, it is possible for result to reach the System.out.prin tln() call without having an explicit value. To fix this, simply set it to zero (or some other default value) when you declare it.

        Also, StringTokenizer has been deprecated as of 1.6 or maybe 1.5. Use the String .split() method instead.

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          Originally posted by NoviceJava
          I'm new to JAVA and need some help writing a simple calculator program.

          Here are the instructions:

          -expects espression with 2 operands together with either +, -, *, or / operator
          -espects only positive input
          -assumes all real #'s
          -assumes operands and operators in expression are separated with spaces

          here is what I have so far:

          import java.io.*;
          import java.util.*;
          public class Calculator
          {
          public static void main(String[] args)
          throws java.io.IOExcep tion
          {
          String inputString, s1, s2, s3;
          float num1, num2, num3, result;

          InputStreamRead er isr = new InputStreamRead er(System.in);

          BufferedReader br = new BufferedReader( isr);

          System.out.prin t("What do you want to compute? ");
          inputString = br.readLine();

          StringTokenizer st = new StringTokenizer (inputString);

          s1 = st.nextToken();
          s2 = st.nextToken();
          s3 = st.nextToken();

          num1 = Float.parseFloa t(s1);
          num2 = Float.parseFloa t(s3);

          if (s2.equals("+") )
          result = (num1 + num2);
          else if (s2.equals("-"))
          result = (num1 - num2);
          else if (s2.equals("/"))
          result = (num1 / num2);
          else if (s2.equals("*") )
          result = (num1 * num2);

          System.out.prin tln("The result is " + result);
          }
          }
          Maybe you can get some idea in this
          thread...

          I hope it helps,
          sukatoa

          Comment

          • NoviceJava
            New Member
            • Mar 2008
            • 14

            #6
            Originally posted by Laharl
            Your compiler is complaining about result because you only assign it values in your if statements. As such, it is possible for result to reach the System.out.prin tln() call without having an explicit value. To fix this, simply set it to zero (or some other default value) when you declare it.

            Also, StringTokenizer has been deprecated as of 1.6 or maybe 1.5. Use the String .split() method instead.
            Thanks, this worked.

            Comment

            • Peterwkc
              New Member
              • Apr 2007
              • 55

              #7
              Happy to hear you solve your problem.

              Comment

              Working...