2016-08-07 1 views
0

c'est mon code pour annuler le bouton quand je l'appuie avant d'appuyer sur le bouton '=' ça marche bien mais après avoir appuyé sur '=' au résultat final est affiché et si le bouton « C » est pressé il est montrant java.lang.NumberFormatException morceau de code ici et « convertir » & « s'is une chaîneerreur après avoir appuyé sur le bouton Annuler 'C' dans la calculatrice (Eclipse IDE et balançoires)

if(e.getSource() == buttonOp){ 
       convert = Integer.parseInt(txt.getText()); 
       System.out.println("parsing done!"); 
       String choice = e.getActionCommand(); 
       if(choice.equals("x^2")){ 
        txt.setText(""); 
        total = convert * convert; 
        System.out.println("total is"+total); 
        s += txt.getText(); 
       } 
       else if(choice.equals("x^3")){ 
        txt.setText(""); 
        total = convert * convert * convert; 
        s += txt.getText(); 
       } 
       else if (choice.equals("=")){ 
       //txt.setText(choice); 
       // number = true; 
        number = false; 
        txt.setText(""); 
        System.out.println("pressed '='"); 
        if(txt.getText().length() > 0){ 
         StringBuilder sb = new StringBuilder(txt.getText()); 
         sb.delete(0, txt.getText().length()); 
         s = sb.toString(); 
        } 
       s = Double.toString(total); 
       s += txt.getText(); 
      } 
       /*else if (choice.equals("+")){ 
        total = convert; 

       }*/ 
       else if(choice.equals("C")){ 
        System.out.println("pressing C"); 
        if(txt.getText().length()>0){ 
         StringBuilder str = new StringBuilder(txt.getText()); 
         str.deleteCharAt(txt.getText().length()-1); 
         s = str.toString(); 
        } 
        txt.setText(s); 
        } 
       } 
      else{ 
       txt.setText(null); 
      } 
      txt.setText(s); 
      } 
     }); 

ici est la sortie que je reçois en appuyant sur » Ç 'après' = '

parsing done! 
    total is2601.0 
    parsing done! 
    pressed '=' 
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:  For input string: "2601.0" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at cal.Calculator$2.actionPerformed(Calculator.java:95) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
+0

Quelle ligne dans votre code est ligne Calculatrice: 95? – rodit

+0

Pour une aide plus rapide, publiez un [MCVE] ou [Short, Self Contained, Example correct] (http://www.sscce.org/). –

Répondre

2

de l'exception

.... parseInt (Source inconnue) ...

... Pour chaîne d'entrée: "2601,0" ...

donc vous appellent parseInt() pour un non int formatée ("2601.0").

Je pense que votre problème est ici

if(e.getSource() == buttonOp){ 
     convert = Integer.parseInt(txt.getText()); // <<<< 

donc soit lieu cela en place plus appropriée (à l'intérieur du if-else) en tant que valeurs non-int ou utiliser Dobule.parseDouble() sont très possible pour une application de calculatrice.