2015-11-03 34 views
2

我在JAVA中相當新,在高中時通過我的計算機科學課程學習它,我遇到了try-catch塊和switch語句的問題。使用switch語句創建try-catch的問題

我正在做的是創建一個轉換程序,根據我要執行的switch語句(即1 =釐米到英寸),在1到8的文本字段中放入一個數字,然後輸入一個值在另一個文本字段(即12)中轉換。

我遇到的問題是,我試圖將switch語句放在try-catch塊中,如果非數字被放入文本字​​段並嘗試運行,則會輸出一個錯誤。

private void buttonConvertActionPerformed(java.awt.event.ActionEvent evt) {            
    //gathering inputs, declaring output 
    double output; 
    int choice = Integer.parseInt(txtfConversion.getText()); 
    double value = Double.parseDouble(txtfToBeConverted.getText()); 

    //decimal format for that long decimal numbers will not appear 
    DecimalFormat df = new DecimalFormat("0.00"); 

    //setting output based on choice 
    try{ 
     switch(choice){ 
      case 1: 
      output = value*2.52; 
      labelOutput.setText(value+" inches = "+df.format(output)+" centimetres"); 
      break; 
     case 2: 
      output = value*30; 
      labelOutput.setText(value+" feet = "+df.format(output)+" centimetres"); 
      break; 
     case 3: 
      output = value*0.91; 
      labelOutput.setText(value+" yards = "+df.format(output)+" metres"); 
      break; 
     case 4: 
      output = value*1.6; 
      labelOutput.setText(value+" miles = "+df.format(output)+" kilometres"); 
      break; 
     case 5: 
      output = value/2.52; 
      labelOutput.setText(value+" centimetres = "+df.format(output)+" inches"); 
      break; 
     case 6: 
      output = value/30; 
      labelOutput.setText(value+" centimetres = "+df.format(output)+" feet"); 
      break; 
     case 7: 
      output = value/0.91; 
      labelOutput.setText(value+" metres = "+df.format(output)+" yards"); 
      break; 
     case 8: 
      output = value/1.6; 
      labelOutput.setText(value+" kilometres = "+df.format(output)+" miles"); 
      break; 
     default: 
      labelOutput.setText("ERROR. Enter a version value!choice and a con"); 
      break; 
     } 
    }catch(NumberFormatException e){ 
    javax.swing.JOptionPane.showMessageDialog(null, "Please enter a numerical value!", "ERROR", javax.swing.JOptionPane.ERROR_MESSAGE); 
    } 
} 

這是我收到的錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1q" 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
at java.lang.Integer.parseInt(Integer.java:580) 
at java.lang.Integer.parseInt(Integer.java:615) 
at convertme.ConvertMe.buttonConvertActionPerformed(ConvertMe.java:199) 
at convertme.ConvertMe.access$000(ConvertMe.java:14) 
at convertme.ConvertMe$1.actionPerformed(ConvertMe.java:68) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6525) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
at java.awt.Component.processEvent(Component.java:6290) 
at java.awt.Container.processEvent(Container.java:2234) 
at java.awt.Component.dispatchEventImpl(Component.java:4881) 
at java.awt.Container.dispatchEventImpl(Container.java:2292) 
at java.awt.Component.dispatchEvent(Component.java:4703) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) 
at java.awt.Container.dispatchEventImpl(Container.java:2278) 
at java.awt.Window.dispatchEventImpl(Window.java:2739) 
at java.awt.Component.dispatchEvent(Component.java:4703) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) 
at java.awt.EventQueue.access$400(EventQueue.java:97) 
at java.awt.EventQueue$3.run(EventQueue.java:697) 
at java.awt.EventQueue$3.run(EventQueue.java:691) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) 
at java.awt.EventQueue$4.run(EventQueue.java:719) 
at java.awt.EventQueue$4.run(EventQueue.java:717) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 
+0

你看到什麼錯誤? – yshavit

+0

我編輯了包含錯誤的帖子,如果我把一個字母放到另一個文本字段中,我會得到相同的錯誤,相同的NumberFormatException,只是說明不同的locatiosn。 –

回答

1

的異常沒有被捕獲,因爲它是try塊之外。嘗試在try塊內移動此行int choice = Integer.parseInt(txtfConversion.getText());

+1

謝謝!現在很好用:) –