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

Thread: Calculator

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Question Calculator

    Hello,
    can someone help me with this code (it is a calculator with ActionListener):

    else if (src == referenca.buttonPlus)//when someone press button +
    			{
     
    num=referenca.getDisplay();//gets the number that someone typed and saves it into variable num
     
    numStringToDouble = Double.parseDouble(num);//Converts String to Double 
     
    matOperacija="+"; //saves mathematical operations - I have set this variable to be INT but I am not sure what should it be INT,CHAR,STRING...
     
    referenca.setDisplay ("",""); //sets blank display 
     
    			}
     
    //here should be buttonMinus, buttonMultpy etc...all the same as buttonPlus
     
    			else if (src == referenca.buttonEqual)
    			{
    String equal= referenca.getDisplay(); //gets displayed number
     
    Double newEqual =Double.parseDouble(equal); //converts displayed number into double
     
    Double result = //here is where I should use a mathematical operation but I do not know how.
     
    String newResult = Double.toString(result); // converts Double into String
     
    referenca.setDisplay(newResult); //sets display to a new result
     
    			}

    My problem is that I do not know how to do this: When I type first number and a mathematical operation that number is saved into variable numStringToDouble and so is mathematical operation saved into matOperacija (not sure what type should I use int or String or Char) and then when I type number two and press equal number on the display should be added, multiplyed etc. whit the number saved in a variable numStringToDouble. My biggest problem is that I do not know how to deal with mathematical operations. How to save them and how to use them.

    I tried to follow SSCCE but if I made a mistake please feel free to point out that mistake.

    Thanks


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Calculator

    You just have to make alot of if and else if statements to calculate what math operator you should use.
    Expecting that you have two values(one that has been varified earlier(when you press the plus button), par1, and one that you get from the TextField(or whatever you are using), par2) that you want to calculate you can use this example.

    EXAMPLE:

    else if (src == buttonEqual) {
         double result;
         double par2 = Double.parseDouble(referenca.getDisplay());
         if (mathOperator.equals("+")) { //checks if the String that holds the math operator equals "+"
              result = par1 + par2;
         }
         else if (mathOperator.equals("-")) {
              result = par1 - par2;
         }
         //Continue the else if statements for all your math operators.
     
         referenca.setDisplay(Double.toString(result));
    }

    Hope this helps!

  3. The Following User Says Thank You to Dr.Code For This Useful Post:

    ikocijan (June 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Calculator

    THANK YOU,VERY MUCH!! that really helped.

    (mathOperator.equals("-"))

    this line is what was bothering me and now I know how to check if string is equal to something.

Similar Threads

  1. Age Calculator
    By Sagittarian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 1st, 2012, 08:06 PM
  2. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  3. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM