我之前有一个关于相同代码的问题,但原因不同。我不希望这个问题太杂乱。以下是供参考的链接:Having trouble with JOPtionPane
我希望检查用户是否在showInputDialog中没有输入任何内容并显示备用消息并将它们带回原始提示,或者只是循环访问同一个对话框,直到它们正常关闭或继续执行输入一个数字。
其余代码通过退出程序而完全按照我想要的方式执行而不会抛出异常。我希望所有inputDialogs的行为方式与我尝试使用测试部分的方式相同,而不仅仅是第一个。
目前,在没有输入的情况下点击“确定”时,它将在退出时抛出NPE异常或清空String错误。
以下是代码(投资1和投资2是静态方法):
public static void main(String[] args)
{
String initialAmt_Str, targetAmt_Str, interestPct_Str, years_Str, result;
double principle = 0, target = 0, interest = 0;
int again = JOptionPane.NO_OPTION, time = 0;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
do {
Object[] options = {"Compute years to reach target amount",
"Compute target amount given number of years"};
int choice = JOptionPane.showOptionDialog(null, "Please choose what you would like to do.",
"Investment Advisor", JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, null);
if (choice != JOptionPane.CANCEL_OPTION)
{
if (choice == 1)
{
initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor",
JOptionPane.PLAIN_MESSAGE);
if (initialAmt_Str != null)
principle = Double.parseDouble(initialAmt_Str);
以下是我正在尝试检查空白字符串
else {
if (initialAmt_Str.isEmpty()){
while (initialAmt_Str.isEmpty()) {
initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor",
JOptionPane.PLAIN_MESSAGE);}}
if (initialAmt_Str != null)
principle = Double.parseDouble(initialAmt_Str);
else
System.exit(0);}
结束测试部分
interestPct_Str = JOptionPane.showInputDialog (null, "Enter the interest rate as a"
+ " percentage (without the percent sign):", "Investment Advisor", JOptionPane.PLAIN_MESSAGE);
if (interestPct_Str != null && !interestPct_Str.isEmpty())
interest = Double.parseDouble(interestPct_Str);
else
System.exit(0);
years_Str = JOptionPane.showInputDialog (null, "Enter the amount of years:", "Investment Advisor",
JOptionPane.PLAIN_MESSAGE);
if (years_Str != null && !years_Str.isEmpty())
time = Integer.parseInt(years_Str);
else
System.exit(0);
result = "Your target amount given the number of years is " +
fmt.format(investment2(principle, interest, time)) + ".";
JOptionPane.showMessageDialog (null, result, "Investment Advisor", JOptionPane.PLAIN_MESSAGE);
again = JOptionPane.YES_OPTION;
}
}
else
again = JOptionPane.NO_OPTION;
if (choice == 0)
{
initialAmt_Str = JOptionPane.showInputDialog (null,"Enter the principle:","Investment Advisor",
JOptionPane.PLAIN_MESSAGE);
if (initialAmt_Str != null && !initialAmt_Str.isEmpty())
principle = Double.parseDouble(initialAmt_Str);
else
System.exit(0);
interestPct_Str = JOptionPane.showInputDialog (null, "Enter the interest rate as a"
+ " percentage (without the percent sign):", "Investment Advisor", JOptionPane.PLAIN_MESSAGE);
if (interestPct_Str != null && !interestPct_Str.isEmpty())
interest = Double.parseDouble(interestPct_Str);
else
System.exit(0);
targetAmt_Str = JOptionPane.showInputDialog (null, "Enter your target amount:", "Investment Advisor",
JOptionPane.PLAIN_MESSAGE);
if (targetAmt_Str != null && !targetAmt_Str.isEmpty())
target = Double.parseDouble(targetAmt_Str);
else
System.exit(0);
result = "You will reach your target amount in " +
investment1(principle, target, interest) +
(investment1(principle, target, interest) == 1 ? " year." : " years.");
JOptionPane.showMessageDialog (null, result, "Investment Advisor", JOptionPane.PLAIN_MESSAGE);
again = JOptionPane.YES_OPTION;
}
if (again != JOptionPane.NO_OPTION)
again = JOptionPane.showConfirmDialog(null, "Find Another?", "", JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
} while (again == JOptionPane.YES_OPTION);
}
}
答案 0 :(得分:3)
只需添加对result.isEmpty()的调用
String result = JOptionPane.showInputDialog ("Input Text Here");
if( result.isEmpty() )
{
// quit the loop
}
答案 1 :(得分:0)
删除if语句 if(initialAmt_Str.isEmpty()),仅使用while循环。当用户输入空字符串或 initialAmt_Str 为空时,while循环将始终为true,但是当用户输入有效输入时,while循环将变为false,从而中断循环
else {
while (initialAmt_Str.isEmpty()) {
initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor",
JOptionPane.PLAIN_MESSAGE);}
if (initialAmt_Str != null)
principle = Double.parseDouble(initialAmt_Str);
else
System.exit(0);}
在这里