Java NumberFormatException
@MadProgrammer,我使用NumberFormatException捕获空格或字符并返回警告消息。它在主页上取得了成功,但在选项“添加t”中,它没有显示每种t恤颜色,而是在第一种颜色(蓝色)周围循环显示。我也尝试过“while”循环语句,但它会导致程序停止。如果它适用于第一部分,display_menu(),我不明白为什么它不适用于“add_t”Java NumberFormatException,java,joptionpane,numberformatexception,Java,Joptionpane,Numberformatexception,@MadProgrammer,我使用NumberFormatException捕获空格或字符并返回警告消息。它在主页上取得了成功,但在选项“添加t”中,它没有显示每种t恤颜色,而是在第一种颜色(蓝色)周围循环显示。我也尝试过“while”循环语句,但它会导致程序停止。如果它适用于第一部分,display_menu(),我不明白为什么它不适用于“add_t” import javax.swing.JOptionPane; 公共类网上商店{ 字符串[]ColorType={“蓝色”、“绿色”、“黑色
import javax.swing.JOptionPane;
公共类网上商店{
字符串[]ColorType={“蓝色”、“绿色”、“黑色”};
最终整数颜色=3;//t选项
int[]颜色=新的int[颜色];
整数和;
public int display_menu(){//不是主程序,而是主菜单。
字符串输入=null;
布尔检验=真;
while(test==true){
试一试{
input=JOptionPane.showInputDialog(“欢迎!”+”\n\n1.添加t订单\n2.编辑t订单\n3.查看当前订单\n4.签出“+”\n\n请输入您的选择:”;
返回整数.parseInt(输入);
}捕获(NumberFormatException nfe){
showMessageDialog(null,“输入必须是数字”);
}
}
返回整数.parseInt(输入);
}
public OnlineStore(){//开关盒程序
布尔退出=假;
做{
开关(显示菜单()){
案例1:
添加_t();
打破
案例2:
退出=真;
打破
默认值://如果遇到错误。
showMessageDialog(null,“哦,天哪!错误!”);
打破
}
}同时(!退出);
}
公共最终整数加法(){
for(int index=0;index
让我们快速查看一下添加
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 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
value。它将一直循环,直到用户输入有效的int
值或按cancel键。该方法将返回一个int
(确切地说是Integer
)或一个null
。null
表示用户按下了取消按钮
现在,您可以简单地使用
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(){
布尔值=假;
for(int index=0;index
要向用户询问
int
值我刚刚将您的建议纳入我的程序中。工作起来很有魅力。感谢您的详细解释和耐心。谢谢你的帮助。我将更加小心地在循环中引入循环。提示输入整数方法非常有用。:-)很高兴这一切都能帮上忙;)
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;
}
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;
}