簡體   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