2015-11-03 89 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

谢谢!现在很好用:) –