Java NumberFormatException

Java NumberFormatException

@MadProgrammer,我使用 NumberFormatException 来捕获空白或字符并返回警告消息。它在主页上是成功的,但对于选项 "add t",它没有显示每种 T 恤颜色,而是围绕第一种颜色蓝色循环显示。我也试过 'while' 循环语句,但它会导致程序停止。如果它适用于第一部分 display_menu(),我不明白为什么它不适用于 "add_t".

import javax.swing.JOptionPane;

public class OnlineStore {

    String[] ColorType = { "blue", "green", "black" };
    final int COLOURS = 3; // t choices
    int[] Color = new int[COLOURS];
    int sum;

    public int display_menu(){ // Not the main program but the main menu.
        String input = null;
        boolean test = true;
        while (test == true) {
            try {
                input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add t order\n2. Edit t order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
                return Integer.parseInt(input);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Input must be a number.");
            }
        }
        return Integer.parseInt(input);
    }

    public OnlineStore(){ // Switch-case program
        boolean exit = false;
        do {
            switch (display_menu()) {
            case 1:
                add_t();
                break;
            case 2:
                exit = true;
                break;
            default: // If an error is encountered.
                JOptionPane.showMessageDialog(null, "Oh dear! Error!");
                break;
            }
        } while (!exit);
    }

    public final int add_t() {
        for (int index = 0; index < ColorType.length; index++) {
            boolean test = true;
            while (test == true) {
                try {
                    String orderItems = JOptionPane.showInputDialog("Please enter your t order for " + ColorType[index]);
                    int items = Integer.parseInt(orderItems);
                    Color[index] = items;
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "Input must be a number.");
                }
            }
        }
        sum = Color[0] + Color[1] + Color[2];
        JOptionPane.showMessageDialog(null, "Your total order is " + sum);
        return Color.length;
    }

    public static void main(String[] args){ // Main program
        new OnlineStore(); // Call out the program.
    }
}

让我们快速浏览一下add_t...

public final int add_t() {
    for (int index = 0; index < ColorType.length; index++) {
        boolean test = true;
        while (test == true) {
            try {
                String orderItems = JOptionPane.showInputDialog("Please enter your t order for " + ColorType[index]);
                int items = Integer.parseInt(orderItems);
                Color[index] = items;
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Input must be a number.");
            }
        }
    }
    sum = Color[0] + Color[1] + Color[2];
    JOptionPane.showMessageDialog(null, "Your total order is " + sum);
    return Color.length;
}

首先,你有一个for-loop,所以你可以提示每个颜色类型,不合理,接下来你有while (test == true),现在,在循环中有一个快速循环,没有退出条件,你在哪里设置 testfalse 这样循环就可以退出了...糟糕,无限循环。

它在您之前的尝试中起作用的原因是,如果 Integer.parseInt 没有产生错误,该值会自动 returned

return Integer.parseInt(input);

现在,我是守旧派,我喜欢一个方法的一个入口点和一个出口点,它可以防止像这样的错误或误解。

相反,由于这似乎是您可能经常做的事情,我会编写一个简单的 "prompt for a integer" 方法,例如...

public Integer promptForInt(String prompt) {
    Integer value = null;
    boolean exit = false;

    do {
        String input = JOptionPane.showInputDialog(prompt);
        if (input != null) {
            try {
                value = Integer.parseInt(input);
            } catch (NumberFormatException exp) {
                JOptionPane.showMessageDialog(null, "Input must be a number.");
            }
        } else {
            exit = true;
        }
    } while (value == null && !exit);

    return value;
}

现在,所有这一切都要求用户提供 int 值。它会一直循环,直到用户输入有效的 int 值或按下取消。该方法将 return 一个 int(准确地说是 Integer)或一个 nullnull 表示用户按下了取消按钮

现在,您只需使用

public int display_menu() // Not the main program but the main menu.
{
    Integer value = promptForInt("Welcome!" + "\n\n1. Add t order\n2. Edit t order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
    return value != null ? value : 4;
}

public final int add_t() {
    boolean canceled = false;
    for (int index = 0; index < ColorType.length; index++) {

        Integer value = promptForInt("Please enter your t order for " + ColorType[index]);
        if (value != null) {
            Color[index] = value;
        } else {
            canceled = true;
            break;
        }
    }
    if (!canceled) {
        sum = Color[0] + Color[1] + Color[2];
        JOptionPane.showMessageDialog(null, "Your total order is " + sum);
    }
    return canceled ? -1 : Color.length;
}

向用户请求 int