为什么尝试抓住我的代码收到错误?

时间:2015-03-20 06:43:49

标签: java try-catch joptionpane

java编程和尝试JOptionPane函数的新功能。在我的代码的最后部分,当用户在年龄时输入字符或字符串时,我会进行验证,并显示无效消息。

这是我的代码:

package activity1;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Activity1 {

    public static void main(String[] args) {

        JOptionPane.showMessageDialog(null, "JOptionPane Activity", "Sample Message Box", JOptionPane.INFORMATION_MESSAGE);

        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        DateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        Calendar time = Calendar.getInstance();
        ImageIcon icon = new ImageIcon("D:\\java\\calendar.jpg");
        JOptionPane.showMessageDialog(null, "today is " + dateFormat.format(date) + "\n" +"and the time is " + timeFormat.format(time.getTime()), "TIME BOX", JOptionPane.INFORMATION_MESSAGE, icon);

        ImageIcon icon2 = new ImageIcon("D:\\java\\question.jpg");
        String[] options = new String[] {"Yes", "No", "Maybe"};
        int selection = JOptionPane.showOptionDialog(null, "Do you want to continue?", "CONFIRMATION MESSAGE", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, icon2, options, options[0]);

        if(selection==0) {
            JOptionPane.showMessageDialog(null, "You've chosen to continue...", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else if(selection==2) {
            JOptionPane.showMessageDialog(null, "Indecision means yes so we will proceed...", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showMessageDialog(null, "You've cancelled the operation,,,", "Message", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String name;
        name = JOptionPane.showInputDialog(null, "Enter your name:", "Sample Inputbox", JOptionPane.WARNING_MESSAGE);
        JOptionPane.showMessageDialog(null, "Hello " + name);

        String age;
        age = JOptionPane.showInputDialog(null, "How old are you?", "Sample Inputbox", JOptionPane.WARNING_MESSAGE);
        int year, born;
        year = Calendar.getInstance().get(Calendar.YEAR);
        born = year - (Integer.parseInt(age));

        try {
            Integer.parseInt(age);
        } 
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid input...Goodbye!", "Sample Message Box", JOptionPane.INFORMATION_MESSAGE);
            return;
        } 

        JOptionPane.showMessageDialog(null, "So you were born on year " + born, "Sample Message Box", JOptionPane.QUESTION_MESSAGE);
    }

}

当我输入" a"这是错误。在年龄:

Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at activity1.Activity1.main(Activity1.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)

3 个答案:

答案 0 :(得分:4)

错误是由于声明

 born = year - (Integer.parseInt(age));

这应该在try - catch

try {
   born = year - (Integer.parseInt(age));
   ...
} catch {
   ...
}

答案 1 :(得分:1)

您解析try catch块之外的字符串:

  born = year - (Integer.parseInt(age));

将它也移动到块中。

    try {
        born = year - (Integer.parseInt(age));
    } 
    catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid input...Goodbye!", "Sample Message Box", JOptionPane.INFORMATION_MESSAGE);
        return;
    } 

答案 2 :(得分:1)

您正在错误地使用try-catch。

born = year - (Integer.parseInt(age));不在try-catch块内,因此它会触发异常。

try-catch中的行也不存储转换结果。

注意:尽量不要在实际应用程序中使用绝对路径。存储与您的应用程序相关的数据,以便在文件位置发生变化时不会出现FileNotFound错误。