如果用户输入随机字母JOptionPane,如何给出对话框错误

时间:2013-07-18 23:49:15

标签: java dialog joptionpane

所以我是java新手,我正在尝试使用try和catch。例如,如果我问用户有多少葡萄,并且他们输入一串字母,它将显示一个错误对话框,而不仅仅是出现系统错误。我能用扫描仪完成,但不能用JOptionPane。我真的想要一个对话框出现,这就是我尝试使用JOptionPane.showInputDialog的原因。

有效的扫描仪...... =

import java.util.Scanner;

class test {
    public static void main (String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.println("How many grapes do u have?");
        int grapes = 1;
        try
        {
            grapes = input.nextInt();
        }
        catch (Exception e)
        {
            System.out.println("Good job Sherlock you broke the program");
            return;
        }
        int mg;

        if (grapes >= 100)
            mg = 1;

        else
            mg = 2;

        switch (mg){
            case 1: System.out.println("You got a lot of grapes");
                break;
            case 2: System.out.println("You brarely got any grapes");
                break;
        }
    }
}

JOptionPane不起作用......

import javax.swing.JOptionPane;

public class bday
{
    public static void main(String[] args)
    {
        String age = "0";
        try
        {
            age = JOptionPane.showInputDialog("What was your age yesterday?");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
            return;
        }
        int iage = Integer.parseInt(age);

        String bday = "0";

         try
         {
             bday = JOptionPane.showInputDialog("Was yesterday your B-Day? (True or False)");
         }
         catch (Exception e)
         {
            JOptionPane.showMessageDialog(null, "WHY U MESS UP PROGRAM???.... BYE BYE!!");
            return;
         }
         boolean bage = Boolean.parseBoolean(bday);
        if (bage == true){
            iage += 1;
            JOptionPane.showMessageDialog(null, "You are now " + iage);
        }
        else if (bage == false){
            JOptionPane.showMessageDialog(null, "Happy unbirthday!");
        }
        if (iage ==10){
            JOptionPane.showMessageDialog(null, "Congrats, double digits!");
        }
        if (iage >19){
            JOptionPane.showMessageDialog(null,  "U aint a Teenager");
        }
        else if (iage < 13)
            JOptionPane.showMessageDialog(null,  "U aint a Teenager");          

    }
}

2 个答案:

答案 0 :(得分:1)

你正在尝试/抓错了。您应该将解析语句放在try块中,因为这会引发异常。

例如,不是

    String age = "0";
    try
    {
        age = JOptionPane.showInputDialog("What was your age yesterday?");
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
        return;
    }
    int iage = Integer.parseInt(age);

而是:

String age = JOptionPane.showInputDialog("What was your age yesterday?");
try {
  iage = Integer.parseInt(age);
} catch (NumberFormatException nfe) {
  // show error
}  

此外,您应该避免捕获Exception,而应该只捕获特定的例外情况,NumberFormatException


修改
在评论中你问:

  

还有一个问题,我应该对布尔值做同样的事吗?

在解析时,布尔类型(在我看来)有点棘手。要查看Boolean.parseBoolean(...)的工作原理,请查看Boolean API,特别是parseBoolean方法。如果输入的文本没有意义,你会发现它不会抛出NumberFormatException。 API将告诉您实际返回 的内容。 try / catch块在这里不起作用。如果您需要捕获错误,请考虑使用String的equalsIgnoreCase(...)

答案 1 :(得分:1)

查看失败的行。我怀疑它是boolean bage = Boolean.parseBoolean(bday);。 您可以随意在String中保存任何内容,但将“whatever”转换为布尔值可能会失败。

把它放在你的try / catch中。