• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help out a noob trying to make a calculator

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Thomas VanHuy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic