aspose file tools*
The moose likes Beginning Java and the fly likes Help out a noob trying to make a calculator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help out a noob trying to make a calculator" Watch "Help out a noob trying to make a calculator" New topic
Author

Help out a noob trying to make a calculator

Thomas VanHuy
Greenhorn

Joined: Jan 03, 2013
Posts: 2
Hi, I really wouldn't know how to fix the problem I'm experiencing, I hope you guys at Javaranch can help me.

What happens when I run the code: the JOptionPane dialog that lets the user choose the operation opens, I can enter the operation, then I click "ok" to go to the next box but my program closes after clicking "ok".

I've tried seeing what could be wrong in my code but I really can't spot what I did wrong.




Thanks to anyone willing to take the time to help me out.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Thomas VanHuy wrote:I can enter the operation, then I click "ok" to go to the next box but my program closes after clicking "ok".

Does it actually close or does it just sit there apparently doing nothing ?
On line 20 the program is waiting for you to input something on the command line. You don't need to do this. The name variable on line 18 contains the value you typed into the dialog. Get rid of all the Scanner objects you create and it should work fine.

Edit: You will of course need to convert the values of the option and option2 variables to doubles before you can use them in any arithmetic. You can use the Double.parseDouble method to do this.


Joanne
Thomas VanHuy
Greenhorn

Joined: Jan 03, 2013
Posts: 2
Joanne Neal wrote:
Thomas VanHuy wrote:I can enter the operation, then I click "ok" to go to the next box but my program closes after clicking "ok".

Does it actually close or does it just sit there apparently doing nothing ?
On line 20 the program is waiting for you to input something on the command line. You don't need to do this. The name variable on line 18 contains the value you typed into the dialog. Get rid of all the Scanner objects you create and it should work fine.

Edit: You will of course need to convert the values of the option and option2 variables to doubles before you can use them in any arithmetic. You can use the Double.parseDouble method to do this.


Is this what you meant?



Because now after entering the operation I get this exception: Exception in thread "main" java.lang.NullPointerException
at rekenmachine1.main(rekenmachine1.java:20)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36520
    
  16
I suggest you stop at this point and consider what you are doing. You are making things difficult for yourself. You are trying to calculate and you are trying input. The two are separate tasks and you should consider them separately. You can see you have confusion because you have the same code:-
String option = JOptionPane.showInputDialog(frame,"Enter the first number of the subtraction" );
Double option3 = new Double(option);


String option2 = JOptionPane.showInputDialog(frame,"Enter the second number of the subtraction" );
Double option4 = new Double(option);
…four times. I suggest you delete all the code which deals with input. I suggest you create a Calculation class (now we are actually getting object‑oriented, which your earlier solution wasn’t), and pass that to the calculator.Get that working, then consider how to put the numbers into the Calculation object later. Take notice of what Joanne said; she is right that you are getting confused with option panes and console input with your Scanner. You only need one, not both. I suggest Scanner. Also search my posts for uses of utility classes and Scanner; that will give you useful hints how you can create a utility class which makes Scanners easier to use.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Thomas VanHuy wrote:Because now after entering the operation I get this exception: Exception in thread "main" java.lang.NullPointerException
at rekenmachine1.main(rekenmachine1.java:20)



Joanne Neal wrote:The name variable on line 18 contains the value you typed into the dialog.

What variable are you testing on line 18 ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7077
    
  16

Campbell Ritchie wrote:I suggest you stop at this point and consider what you are doing.

My, my. Agreeing with Campbell twice in two days; must be a record...

@Thomas: ...but he's absolutely right: StopCoding (←click). In fact, whenever you run into difficulties you should get into the habit of doing it.

He's also right about your repeated code. Good programmers share a common trait: they hate typing - or at least, they hate typing the same thing more than once. I've been programming for 35 years, and I still only use two fingers.

And copy/paste is NOT the solution. Indeed, I wish more IDEs would disable the function in their source editors.

Tip: Whenever you find yourself typing the same thing more than once, you should ask yourself: How can I put this in a method (or, as he suggests, a class)?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help out a noob trying to make a calculator
 
Similar Threads
possible loss of precision
Triangle Perimeter HELP!!!
HELP!!!! how does Character.isLetter work? (need experts)
Problem with Sort Method
Erroneous sym type What does this mean?