Java Calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LolaT
    New Member
    • Jul 2007
    • 22

    Java Calculator

    I have just started using java and I'm trying to make a simple calculator, but I'm confused as to why I keep getting a warning message.

    [code=java]import java.util.Scann er;

    public class Calculator {




    public static void main(String[] args) {
    Scanner sc=new Scanner(System. in);
    System.out.prin tln("Enter first number: ");
    double d1=sc.nextDoubl e();
    System.out.prin tln("Enter second number: ");
    double d2=sc.nextDoubl e();


    }

    }[/code]


    it tells me that my variables d1 and d2 are never read...I have no idea what this means...can someone please help me or refer me to a good book or SOMETHING...I'm totally lost.
    Thanks!
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    #2
    Originally posted by LolaT
    I have just started using java and I'm trying to make a simple calculator, but I'm confused as to why I keep getting a warning message.

    [code=java]import java.util.Scann er;

    public class Calculator {




    public static void main(String[] args) {
    Scanner sc=new Scanner(System. in);
    System.out.prin tln("Enter first number: ");
    double d1=sc.nextDoubl e();
    System.out.prin tln("Enter second number: ");
    double d2=sc.nextDoubl e();


    }

    }[/code]


    it tells me that my variables d1 and d2 are never read...I have no idea what this means...can someone please help me or refer me to a good book or SOMETHING...I'm totally lost.
    Thanks!
    it doesn't matter if you get warning on the code.
    You declare&initial ise d1 and d2 but not yet use them in your code...so there are warning show up.

    Comment

    • LolaT
      New Member
      • Jul 2007
      • 22

      #3
      Originally posted by nickyeng
      it doesn't matter if you get warning on the code.
      You declare&initial ise d1 and d2 but not yet use them in your code...so there are warning show up.

      Thanks for the quick reply...I think I figured that part out...now I'm having a problem adding if statements...I' m trying to say if my operator is equal to '+' then the 2 numbers should be added. Like, I said, I'm very new at this, so I apologize if I make stupid mistakes...this is what I have so far:

      [code=java]System.out.prin tln("Enter an operator (+ - * / %): ");
      String x= sc.next();
      if (x== +)
      System.out.prin tln("The result: " + (d1+d2));[/code]

      My error message says:

      Exception in thread "main" java.lang.Error : Unresolved compilation problem:
      Syntax error on token "+", Expression expected after this token

      at Calculator.main (Calculator.jav a:21)

      Comment

      • nickyeng
        Contributor
        • Nov 2006
        • 252

        #4
        Originally posted by LolaT
        Thanks for the quick reply...I think I figured that part out...now I'm having a problem adding if statements...I' m trying to say if my operator is equal to '+' then the 2 numbers should be added. Like, I said, I'm very new at this, so I apologize if I make stupid mistakes...this is what I have so far:

        [code=java]System.out.prin tln("Enter an operator (+ - * / %): ");
        String x= sc.next();
        if (x== +)
        System.out.prin tln("The result: " + (d1+d2));[/code]

        My error message says:

        Exception in thread "main" java.lang.Error : Unresolved compilation problem:
        Syntax error on token "+", Expression expected after this token

        at Calculator.main (Calculator.jav a:21)
        if your if statement, you should put like this:

        [code=java]
        if(x.equalsIgno reCase("+"))
        {
        System.out.prin tln("The result is " + (d1+d2)) ;
        }
        [/code]
        Last edited by JosAH; Sep 18 '07, 06:21 AM. Reason: added a right parenthesis

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by LolaT
          Thanks for the quick reply...I think I figured that part out...now I'm having a problem adding if statements...I' m trying to say if my operator is equal to '+' then the 2 numbers should be added. Like, I said, I'm very new at this, so I apologize if I make stupid mistakes...this is what I have so far:

          [code=java]System.out.prin tln("Enter an operator (+ - * / %): ");
          String x= sc.next();
          if (x== +)
          System.out.prin tln("The result: " + (d1+d2));[/code]

          My error message says:

          Exception in thread "main" java.lang.Error : Unresolved compilation problem:
          Syntax error on token "+", Expression expected after this token

          at Calculator.main (Calculator.jav a:21)
          First of all, you want "+" to be a String, as that's what you want to compare it to.

          Next, you can't use "==" to compare Strings. The Reason is, that every String is an Object in Java and you create two Objects: Your input and the "+", which is in your code. As those two Objects aren't the same, that comparison must result in false.

          To compare Strings, use "+".equals( x) or similar.

          Greetings,
          Nepomuk

          Comment

          • LolaT
            New Member
            • Jul 2007
            • 22

            #6
            thanks to everyone for their help, i think i'm slowly getting the hang of it.

            I've configured my calculator so that it works as it is supposed to (for the most part), but now I'm trying to add a statement that will continue the process of evaluating the numbers entered should the user choose to continue.

            So far, here is my code:

            [code=java]
            import java.util.Scann er;

            public class Calculator {




            public static void main(String[] args) {
            Scanner sc=new Scanner(System. in);
            System.out.prin tln("Enter first number: ");
            double d1=sc.nextDoubl e();
            System.out.prin tln("Enter second number: ");
            double d2=sc.nextDoubl e();
            System.out.prin tln("Enter an operator (+ - * / %): ");
            String x= sc.next();
            if (x.equalsIgnore Case("+"))
            System.out.prin tln("The result: " + (d1+d2));

            else if (x.equalsIgnore Case("-"))
            System.out.prin tln("The result: " + (d1-d2));

            else if (x.equalsIgnore Case("*"))
            System.out.prin tln("The result: " + (d1*d2));

            else if (x.equalsIgnore Case("/"))
            System.out.prin tln("The result: " + (d1/d2));

            else if (x.equalsIgnore Case("%"))
            System.out.prin tln("The result: " + (d1/d2));

            String option=sc.next( );
            System.out.prin tln("Again? (y/n): ");
            [/code]

            I'm pretty sure the way I did it is strenuous and there is an easier way to do it, but since I am new at this, I'm not really comfortable with loops and such.

            If anyone can help me out with how to loop the calculator that would be great...and is there any way to simplify how to get the result?

            I haven't gotten much sleep since trying to figure this out and my brain isn't functioning to it's full potential, so any help would be great!

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by LolaT
              thanks to everyone for their help, i think i'm slowly getting the hang of it.
              Brilliant! That's what we want! :-)
              Originally posted by LolaT
              I've configured my calculator so that it works as it is supposed to (for the most part), but now I'm trying to add a statement that will continue the process of evaluating the numbers entered should the user choose to continue.
              So what you want is something like this:
              Code:
              Please enter the first number: 6
              Please enter the second number: 9
              Please enter the operator: +
              6 * 0 = 54
              Do you want to continue (y/n)? y
              Your first number is 54.
              Please enter your second number: 4
              Please enter your operator: -
              54 - 4 = 50
              Do you want to continue (y/n)?
              etc, is that right?
              Originally posted by LolaT
              I'm pretty sure the way I did it is strenuous and there is an easier way to do it, but since I am new at this, I'm not really comfortable with loops and such.
              Loops are the right idea. This article will give you an idea of what loops can do and how they are used. I would suggest to use a while-loop or a do-while-loop and a boolean, which tells the loop if it should continue or not.

              Also, you might want to create a calculate method which does the actual calculating - that would be a bit nicer that what you have now.

              Greetings,
              Nepomuk

              Comment

              • LolaT
                New Member
                • Jul 2007
                • 22

                #8
                Originally posted by nepomuk
                Brilliant! That's what we want! :-)
                So what you want is something like this:
                Code:
                Please enter the first number: 6
                Please enter the second number: 9
                Please enter the operator: +
                6 * 0 = 54
                Do you want to continue (y/n)? y
                Your first number is 54.
                Please enter your second number: 4
                Please enter your operator: -
                54 - 4 = 50
                Do you want to continue (y/n)?
                etc, is that right?
                Loops are the right idea. This article will give you an idea of what loops can do and how they are used. I would suggest to use a while-loop or a do-while-loop and a boolean, which tells the loop if it should continue or not.

                Also, you might want to create a calculate method which does the actual calculating - that would be a bit nicer that what you have now.

                Greetings,
                Nepomuk



                Thanks for the article link!
                Actually, for my program, if the user chooses to run it again, the input should be 2 brand new numbers, so for example:

                Please enter the first number: 6
                Please enter the second number: 9
                Please enter the operator: +
                6 + 9 = 15
                Do you want to continue (y/n)? y
                Please enter the first number: 4
                Please enter the second number: 4
                Please enter the operator: -
                4 - 4 = 0
                Do you want to continue (y/n)?


                Also, is there a way to make a statement that has 4 arguments?
                Perhaps if I was to write an error message had the user not chosen a correct operator, is there any way to say if x is not equal to ____ or ____ or ____ or ____, then print an error message?

                Sorry if that doesn't make much sense! I didn't know how to word it!

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by LolaT
                  Thanks for the article link!
                  You're welcome of course! :-)
                  Originally posted by LolaT
                  Actually, for my program, if the user chooses to run it again, the input should be 2 brand new numbers
                  You can do that with loops too (it's even a bit easier than the example I gave).
                  Originally posted by LolaT
                  Also, is there a way to make a statement that has 4 arguments?
                  If you want to have more than two arguments, you'll either have to change your code to take exactly four arguments, or you'll need some kind of mechanism for checking, how many arguments the user wants to have. If you want a variable amount, you'll certainly need some sort of collection (e.g. array, vector, list, ...). Also, you need to think about how you want to calculate - should
                  Code:
                  1 + 2 * 3 + 4
                  be resolved with
                  Code:
                  1 + 2 * 3 + 4 = 1 + 6 + 4 = 7 + 4 = 11
                  or with
                  Code:
                  1 + 2 * 3 + 4 = 3 * 3 + 4 = 9 + 4 = 13
                  or even with
                  Code:
                  1 + 2 * 3 + 4 = 3 * 3 + 4 = 3 * 7 = 21
                  or any other way? Maybe you even want to support brackets? This is important, as your program must know, what to do first.

                  Basically, you'll have to first collect the whole information and then calculate. (You can do it differently, but it makes things so much more complicated.)
                  Originally posted by LolaT
                  Perhaps if I was to write an error message had the user not chosen a correct operator, is there any way to say if x is not equal to ____ or ____ or ____ or ____, then print an error message?
                  Sure you can Check, if x is one of your possibilities - you can check it like this:
                  [CODE=java]
                  if(!(x.equals(" +")) && !(x.equals("-")) && !(x.equals("*") ) && !(x.equals("/")) && !(x.equald("%") ))
                  // print your message
                  [/CODE]However, you already are checking, if it's one of the known signs with
                  [CODE=java]
                  if (x.equalsIgnore Case("+"))
                  System.out.prin tln("The result: " + (d1+d2));

                  else if (x.equalsIgnore Case("-"))
                  System.out.prin tln("The result: " + (d1-d2));

                  else if (x.equalsIgnore Case("*"))
                  System.out.prin tln("The result: " + (d1*d2));

                  else if (x.equalsIgnore Case("/"))
                  System.out.prin tln("The result: " + (d1/d2));

                  else if (x.equalsIgnore Case("%"))
                  System.out.prin tln("The result: " + (d1/d2));
                  [/CODE]so if you just make another case
                  [CODE=java]
                  else
                  // print your error
                  [/CODE]you will have it done. If you are using a second method for the calculation and are giving back the result of that, you can do with Exceptions.

                  I hope, that helps.

                  Greetings,
                  Nepomuk

                  Comment

                  • LolaT
                    New Member
                    • Jul 2007
                    • 22

                    #10
                    Originally posted by nepomuk
                    You're welcome of course! :-)
                    You can do that with loops too (it's even a bit easier than the example I gave).
                    If you want to have more than two arguments, you'll either have to change your code to take exactly four arguments, or you'll need some kind of mechanism for checking, how many arguments the user wants to have. If you want a variable amount, you'll certainly need some sort of collection (e.g. array, vector, list, ...). Also, you need to think about how you want to calculate - should
                    Code:
                    1 + 2 * 3 + 4
                    be resolved with
                    Code:
                    1 + 2 * 3 + 4 = 1 + 6 + 4 = 7 + 4 = 11
                    or with
                    Code:
                    1 + 2 * 3 + 4 = 3 * 3 + 4 = 9 + 4 = 13
                    or even with
                    Code:
                    1 + 2 * 3 + 4 = 3 * 3 + 4 = 3 * 7 = 21
                    or any other way? Maybe you even want to support brackets? This is important, as your program must know, what to do first.

                    Basically, you'll have to first collect the whole information and then calculate. (You can do it differently, but it makes things so much more complicated.)

                    Sure you can Check, if x is one of your possibilities - you can check it like this:
                    [CODE=java]
                    if(!(x.equals(" +")) && !(x.equals("-")) && !(x.equals("*") ) && !(x.equals("/")) && !(x.equald("%") ))
                    // print your message
                    [/CODE]However, you already are checking, if it's one of the known signs with
                    [CODE=java]
                    if (x.equalsIgnore Case("+"))
                    System.out.prin tln("The result: " + (d1+d2));

                    else if (x.equalsIgnore Case("-"))
                    System.out.prin tln("The result: " + (d1-d2));

                    else if (x.equalsIgnore Case("*"))
                    System.out.prin tln("The result: " + (d1*d2));

                    else if (x.equalsIgnore Case("/"))
                    System.out.prin tln("The result: " + (d1/d2));

                    else if (x.equalsIgnore Case("%"))
                    System.out.prin tln("The result: " + (d1/d2));
                    [/CODE]so if you just make another case
                    [CODE=java]
                    else
                    // print your error
                    [/CODE]you will have it done. If you are using a second method for the calculation and are giving back the result of that, you can do with Exceptions.

                    I hope, that helps.

                    Greetings,
                    Nepomuk

                    Thanks a lot for all the advice! I officially got my loop (and my calculator) working! I've stumbled upon a new problem though....how do I assign the result of my calculator to a new variable? For example:

                    First number: 5
                    Second number: 9
                    Operation (+ - / * %): +
                    The result: 14
                    Again? (y/n): y
                    Fist number: _
                    Second number: 1
                    Operation (+ - / * %): -
                    The result: 13


                    So, when the user puts the "_" symbol, the previous result (meaning 14) is used as the first number.

                    Thanks again!

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      Originally posted by LolaT
                      Thanks a lot for all the advice! I officially got my loop (and my calculator) working! I've stumbled upon a new problem though....how do I assign the result of my calculator to a new variable? For example:

                      First number: 5
                      Second number: 9
                      Operation (+ - / * %): +
                      The result: 14
                      Again? (y/n): y
                      Fist number: _
                      Second number: 1
                      Operation (+ - / * %): -
                      The result: 13


                      So, when the user puts the "_" symbol, the previous result (meaning 14) is used as the first number.

                      Thanks again!
                      You're very welcome of course.

                      For the next task, you'll have to create a variable, which contains the previous result. At the beginning, this could contain 0.

                      Then you'll have to find out, if the user has entered a Number or not. React to that by using either the input or the result saved before.

                      That should be enough to get you started. ^^

                      Greetings,
                      Nepomuk

                      Comment

                      • forse
                        New Member
                        • Sep 2007
                        • 7

                        #12
                        Hi,

                        Can see that you have been progressing in your calculator app so far.

                        If you want a bit more of a reference for a full fledged yet simple calculator application then you may find this link useful.

                        A Free Calculator application written in JAVA

                        Good luck

                        Comment

                        Working...