解析异常;必须被捕获(Try/Catch)(Java)[重复]

2023-12-20

我正在编写预约程序并收到以下错误:

AppointmentNew.java:68: unreported exception java.text.ParseException; must be caught or declared to be thrown
        Date lowDate = sdf.parse(stdin.nextLine());
                                ^
AppointmentNew.java:70: unreported exception java.text.ParseException; must be caught or declared to be thrown
        Date highDate = sdf.parse(stdin.nextLine());  
                                 ^
AppointmentNew.java:77: unreported exception java.text.ParseException; must be caught or declared to be thrown
           Date newCurrentDate = sdf.parse(currentDate); 

我很确定我需要尝试/捕获,但我不确定如何生成它。我收到错误的部分是它要求用户输入开始日期和结束日期,当他们这样做时,程序会打印出他们在两个日期之间进行的约会。

这是我的代码:

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AppointmentNew 
{
public static void main (String[] args)
{
  ArrayList<String> list = new ArrayList<String>();
  Scanner stdin = new Scanner(System.in);
  String choice = "";
  int choiceNum = 0;
  String date = "";
  String descrip = "";
  int type = 0;
  String typeChose = "";

  System.out.println("Welcome to Appointment App!\n");
  System.out.println("\t============================");

  do
  {
     System.out.print("\n\tMake Choice (1: New, 2: Print Range, 3: Print All, 4: Quit) ");
     choice = stdin.nextLine();
     choiceNum = Integer.parseInt(choice);

     if (choiceNum == 1)
     {
        System.out.print("\n\n\tEnter New Appointment Date in mm/dd/yyyy format: ");
        date = stdin.nextLine();

        System.out.print("\n\n\tEnter New Appointment Description: ");
        descrip = stdin.nextLine();

        System.out.print("\n\n\tEnter Type (1 = Once, 2 = Daily, 3 = Monthly): ");
        type = stdin.nextInt();
        stdin.nextLine();

        if (type == 1)
        {
           Once once = new Once(date, descrip);
           typeChose = "One-Time";
        }
        else if (type == 2)
        {
           Daily daily = new Daily(date, descrip);
           typeChose = "Daily";
        }
        else
        {
           Monthly monthly = new Monthly(date, descrip);
           typeChose = "Monthly";
        }
        String stringToAdd = "";
        stringToAdd = (date + " : \"" + descrip + "\", " + typeChose);
        list.add(stringToAdd);

        System.out.println("\n\n\tNew " + typeChose + " Appointment Added for " + date + "\n");
        System.out.println("\t============================\n");


     }

     if (choiceNum == 2)
     {
        System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date lowDate = sdf.parse(stdin.nextLine());
        System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");
        Date highDate = sdf.parse(stdin.nextLine());  

        for(int i = 0; i < list.size(); i++)
        {
           int dateSpot = list.get(i).indexOf(" ");
           String currentDate = list.get(i);
           currentDate.substring(0, dateSpot);
           Date newCurrentDate = sdf.parse(currentDate); 

           if (newCurrentDate.compareTo(lowDate) >= 0 && newCurrentDate.compareTo(highDate) <= 0)
           {
              System.out.println("\n\t" + list.get(i));   
           }
        }
     }

     if (choiceNum == 3)
     {
        for(int i = 0; i < list.size(); i++)
        {
           System.out.println("\n\t" + list.get(i));     
        }
     }

  }while (choiceNum != 4);      
 }
}

格式为:

try {
 //The code you are trying to exception handle
}
catch (Exception e) {
 //The handling for the code
}

将解析调用放入 try 块中(最好每个调用都放在自己的 try 块中),然后在 catch 块中指定解析失败时应该发生的情况。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解析异常;必须被捕获(Try/Catch)(Java)[重复] 的相关文章

随机推荐