有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么要尝试捕捉我收到的代码错误?

了解java编程并尝试JOptionPane函数。在代码的最后一部分,当用户在age上输入字符或字符串时,我进行了验证,并将显示失效消息

这是我的代码:

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);
    }

}

这是我在age上输入“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)

共 (1) 个答案

  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;
        }