Click here to Skip to main content
15,881,844 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used this code in netbeans to design calculator but the is problem with the division sign immediately I divided the number eg 9 / 8 will give me 1 but when i divided the number eg. 8/2 will give me the correct answer since there is no reminder

Java
private void jBtnDivActionPerformed(java.awt.event.ActionEvent evt) {
      firstnum = Double.parseDouble(jtxtDisplay.getText());
      jtxtDisplay.setText("");
      operations="/"


else if (operations == "/")
   {
       result = firstnum / secondnum;
       answer = String.format("%.0f", result);
       jtxtDisplay.setText(answer);
Posted
Updated 17-Jul-15 3:45am
v3
Comments
ZurdoDev 17-Jul-15 8:33am    
How is secondnum defined?
E.F. Nijboer 17-Jul-15 9:20am    
looks like an else without an if
Richard MacCutchan 17-Jul-15 9:46am    
This is nothing to do with netbeans, it's a Java question. Time to learn the difference.

1 solution

Sounds like you're dividing two integers:

Be careful when performing integer division. When dividing an integer by an integer, the answer will be an integer (not rounded).

Compare these divisions: (5 is an integer while 5.0 is a double)

Integer division 8 / 5 = 1
Double division 8.0 / 5.0 = 1.6
Mixed division 8.0 / 5 = 1.6

Make sure at least one of the numbers is a double before dividing.
 
Share this answer
 
Comments
Salar Hafezi 19-Jul-15 8:50am    
Right solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900