Java NumberFormatException

时间:2015-08-20 02:54:32

标签: java joptionpane numberformatexception

@MadProgrammer,我使用NumberFormatException来捕获空格或字符并返回一条警告消息。它对于主页面是成功的,但对于选项,“添加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.
    }
}

1 个答案:

答案 0 :(得分:1)

让我们快速浏览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所以循环可以退出... opps,无限循环。

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

return Integer.parseInt(input);

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

相反,因为这看起来像你可能会做很多事情,我会写一个简单的“提示整数”方法,比如......

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值或按下取消。该方法将返回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