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

Thread: Windows Calculator.

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Windows Calculator.

    Im trying to create a calculator similar to the windows standard calculator where in when you click "5" and then clicked "+" by mistake you just have to click "-" for the output to be "5 -" not "5 +". Problem is i don't know howto do that with my current code.

    try
    				{	
    					if(inpuDisplay.contains("."))
    					{
    						doubleResult = new Double(Double.parseDouble(inpuDisplay)).toString();
     
    						inpuDisplay = doubleResult;
    					}
     
    					else
    					{
    						doubleResult = new Integer(Integer.parseInt(inpuDisplay)).toString();
     
    						inpuDisplay = doubleResult;
    					}
     
    					resDisplay = inpuDisplay + " + ";
     
    					res2Display = res2Display + resDisplay;
     
    					inpu2Display = display.getText().toString();
     
    					input.setText(res2Display);
     
     
    					display.setText("");
     
    					if(add == true)
    					{
    						result = result + Double.parseDouble(inpu2Display);
     
    						minus = false;
    						mul = false;
    						div = false;
    					}
     
    					if(minus == true)
    					{			
    						if(result == 0)
    						{
    							result = Double.parseDouble(inpu2Display);
    						}
     
    						else if(result < 0)
    						{
    							result = result - Double.parseDouble(inpu2Display);
    						}
     
    						else if(result > 0)
    						{
    							result = result - Double.parseDouble(inpu2Display);
    						}
     
    						minus = false;
    						add = true;
    					}
     
    					if(mul == true)
    					{
    						if(result == 0)
    						{
    							result = Double.parseDouble(inpu2Display);
    						}
     
    						else
    						{
    							result = result * Double.parseDouble(inpu2Display);
    						}
     
    						mul = false;
    						add = true;
    					}
     
    					if(div == true)
    					{
    						if(result == 0)
    						{
    							result = Double.parseDouble(inpu2Display);
    						}
     
    						else
    						{
    							result = result / Double.parseDouble(inpu2Display);
    						}					
     
    						div = false;
    						add = true;
    					}
     
    					if(result % 1 == 0)
    					{
    						display.setText(Integer.toString((int)result));
    					}
     
    					else
    					{
    						display.setText(Double.toString(result));
    					}
     
    					inpuDisplay = "";
    					inpu2Display = "";
    				}
     
    				catch(Exception e)
    				{
     
    				}

    NOTE: The above code is executed when yhe "+" button is clicked. And is working i just have to add the replace sign function. Could someone please help?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Windows Calculator.

    What exactly do you need help with? You've described your goal, but no what you're confused about. You also haven't provided an SSCCE (note that this should be as small as possible, not your whole program, but should also be runnable by us).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Windows Calculator.

    I agree with all KW said, but I'm also confused why this is a problem. The action listeners for the arithmetic operation keys should be programmed to do what you've described.

  4. #4
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Android Calculator like Windows Calculator

    Im trying to create a calculator similar to the windows standard calculator where in when you click "5" and then clicked "+" by mistake you just have to click "-" for the output to be "5 -" not "5 +". Problem is i don't know howto do that with my current code.

    try
    				{	
    					if(inpuDisplay.contains("."))
    					{
    						doubleResult = new Double(Double.parseDouble(inpuDisplay)).toString();
     
    						inpuDisplay = doubleResult;
    					}
     
    					else
    					{
    						doubleResult = new Integer(Integer.parseInt(inpuDisplay)).toString();
     
    						inpuDisplay = doubleResult;
    					}
     
    					resDisplay = inpuDisplay + " + ";
     
    					res2Display = res2Display + resDisplay;
     
    					inpu2Display = display.getText().toString();
     
    					input.setText(res2Display);
     
     
    					display.setText("");
     
    					if(add == true)
    					{
    						result = result + Double.parseDouble(inpu2Display);
     
    						minus = false;
    						mul = false;
    						div = false;
    					}
     
    					if(minus == true)
    					{			
    						if(result == 0)
    						{
    							result = Double.parseDouble(inpu2Display);
    						}
     
    						else if(result < 0)
    						{
    							result = result - Double.parseDouble(inpu2Display);
    						}
     
    						else if(result > 0)
    						{
    							result = result - Double.parseDouble(inpu2Display);
    						}
     
    						minus = false;
    						add = true;
    					}
     
    					if(mul == true)
    					{
    						if(result == 0)
    						{
    							result = Double.parseDouble(inpu2Display);
    						}
     
    						else
    						{
    							result = result * Double.parseDouble(inpu2Display);
    						}
     
    						mul = false;
    						add = true;
    					}
     
    					if(div == true)
    					{
    						if(result == 0)
    						{
    							result = Double.parseDouble(inpu2Display);
    						}
     
    						else
    						{
    							result = result / Double.parseDouble(inpu2Display);
    						}					
     
    						div = false;
    						add = true;
    					}
     
    					if(result % 1 == 0)
    					{
    						display.setText(Integer.toString((int)result));
    					}
     
    					else
    					{
    						display.setText(Double.toString(result));
    					}
     
    					inpuDisplay = "";
    					inpu2Display = "";
    				}
     
    				catch(Exception e)
    				{
     
    				}

    NOTE: The above code is executed when yhe "+" button is clicked. And is working i just have to add the replace sign function. Could someone please help?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Android Calculator like Windows Calculator

    Please don't start duplicate topics.

    Continue working this problem in the original post, adding any clarifications and changes in direction there as needed. If that original topic needs to be moved to another subforum, please ask a mod to do that for you.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    jps (August 28th, 2013)

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Windows Calculator.

    Threads merged

  8. #7
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Windows Calculator.

    opss sorry. just found out that there is an android section here. coz my im doing this in Eclipse for my android project.

Similar Threads

  1. Did you know? Screen loggon on Windows 7
    By Andrew R in forum Totally Off Topic
    Replies: 0
    Last Post: August 17th, 2013, 10:58 AM
  2. J2SE for windows ce 6.0
    By djoleasterix in forum Java IDEs
    Replies: 1
    Last Post: March 14th, 2012, 11:25 AM
  3. acces denied on windows 7 ??
    By mnemonik2001 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 25th, 2011, 03:43 PM
  4. Windows Bluetooth Application
    By SarahVeni in forum Java Theory & Questions
    Replies: 1
    Last Post: July 1st, 2011, 01:15 AM
  5. Overlapping windows? in a Jframe?
    By chronoz13 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 21st, 2009, 12:01 PM