繁体   English   中英

使用 switch 语句创建 try-catch 的问题

[英]Issues creating try-catch with switch statement

我在 JAVA 方面相当新,通过我在高中的计算机科学课程学习它,我遇到了我的 try-catch 块和 switch 语句的问题。

我正在做的是创建一个转换程序,根据我要执行的 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)

异常没有被捕获,因为它在 try 块之外。 尝试移动这一行int choice = Integer.parseInt(txtfConversion.getText()); 在 try 块内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM