This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/1/23

java lang NumberFormatException- help

divinity divinity

2017/1/23

#
hi I have build this calculation program, one tab is a scientific, one tab is a standard and the other is a binary. havent started to code the binary nor the scientific part as yet but I have finished coded the standard part already. The errror that I am experiencing as stated in the caption above, is where, everytime I tried to calculated any double digit, i got that error: java.lang.NumberFormatException. if i tried to either divide, add, substract or multiply any double digit, I would got that error, when I either add, substract, or multiply it works fine but when i tried to divide single numbers, I got that error. here is the codes that I used
 private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {                                          
      display.setText(display.getText() +" 1 ");
    }                                         

    private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        display.setText(display.getText() + " 2 ");
    }                                         

    private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        display.setText(display.getText() + " 3");
    }                                         

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
     display.setText(display.getText() + " 4 ");
    }                                         

    private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        display.setText(display.getText() + " 5 ");
    }                                         

    private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {                                          
       display.setText(display.getText() + " 6 ");
    }                                         

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        display.setText(display.getText() + " 7 ");
    }                                         

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        display.setText(display.getText() + " 8 ");
    }                                         

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        display.setText(display.getText() + " 9 ");
    }                                         

    private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) {                                          
       display.setText(display.getText() + " 0");
    }                                         

    private void jButton13AdditionActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        firstDouble = (Double.parseDouble(String.valueOf(display.getText())));
        display.setText("");
        plusClick = 1;
      
    }                                                 
    private void jButton15SubstractionActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        firstDouble = (Double.parseDouble(String.valueOf(display.getText())));
        display.setText("");
       minusClick = 1;
       
    }                                                     

    private void jButton19MultiplyActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        firstDouble = (Double.parseDouble(String.valueOf(display.getText())));
        display.setText("");
        multiplyClick = 1;
       
    }                                                 
    private void jButton23DivideActionPerformed(java.awt.event.ActionEvent evt) {                                                
       firstDouble = (Double.parseDouble(String.valueOf(display.getText())));(this is where the problem really lies,(java.lang.numberformatexception
        display.setText("");
        divideClick= 1;
       
    }                                               

    private void jButton25ActionPerformed(java.awt.event.ActionEvent evt) {                                          
       display.setText(display.getText() + " . ");
    }                                         

    private void jButton12ClearActionPerformed(java.awt.event.ActionEvent evt) {                                               
       display.setText(" ");
    }                                              

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        disable();//to call the disable method
    }                                             

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        enable();//to call the enable method
    }   
divinity divinity

2017/1/26

#
I am currently await for any response
danpost danpost

2017/1/26

#
divinity wrote...
I am currently await for any response
You are, first off, putting spaces in the text between your numbers. They will have to be removed before any multi-digit values can be parsed from the string. For single digit values, you could try using the 'trim' method on the string before parsing:
firstDouble = (Double.parseDouble(String.valueOf(display.getText().trim())));
Try it and see if that was indeed the problem (should only work for single-digit numbers).
divinity divinity

2017/1/26

#
I am still having the same problem even though I added what u just did for me, I believe it have something to do with the sign like the +,*,-,/ and the codes that is, firstDouble
firstDouble=(Double.parseDouble(String.valueOf(display.getText().trim())));
( the error has something to do with this code because ever time the error come on, it point to this line of codes in the calculator
danpost danpost

2017/1/26

#
divinity wrote...
I am still having the same problem even though I added what u just did for me, I believe it have something to do with the sign like the +,*,-,/ and the codes that is, firstDouble
Adding 'trim' will only work to get the first digit accepted in the string. This is long before any operators are dealt with. Is this calculator going to be for just simple "value-operator-value-equals" computations or are you going to allow grouping with parenthesis and chaining of operations -- because this could get a whole lot more complicated, if so. For the simple basic operations, maybe it would be best to have the display object hold the values and the operator (and a result field) and display them accordingly, as needed. Then you can have a method to receive a value and another to receive an operator and another to get any result. The 'getText' method could determine which value to return by checking for the operator; if present, the second value is returned; otherwise, the first one is. Same with the 'setText' method, as far as which value to set.
divinity divinity

2017/1/26

#
it will be more or less for value-operator-value-equals computation(doh know what tha means thou
You need to login to post a reply.