ParseException Java
Asked Answered
G

2

10

I am writing an appointment program that allows the user to input appointment dates, description, and type of appointment. Everything works correctly until they make a selection to "Print Range" which prints a range of dates, when they choose to do this it tells them to enter START date and an END date, the program then pulls all of the appointments from between those dates and displays them into the output box.

Here are the errors I am getting with print range :

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

I think I should probably do a try/catch block but am not real sure of how to do that, and was wondering if someone could provide me an answer or example to fix these errors.

Here is some of my code where I believe the parse error is taking place :

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

public class AppointmentNew 
{
public static void main (String[] args) throws Exception
{

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

           }
        }
     }
Garget answered 20/4, 2013 at 4:44 Comment(0)
H
13

Parse Exception is checked exception so you must have to handle it. Either by throws or try catch block.

public static void main (String[] args)

Should be

public static void main (String[] args) throws ParseException

Or in try catch block

try {
    //All your parse Operations
} catch (ParseException e) {
   //Handle exception here, most of the time you will just log it.
   e.printStackTrace();
  }
Heine answered 20/4, 2013 at 4:46 Comment(1)
@jmsutton334 I made some edit in my answer . If it works the way you wanted you can accept my answer.Heine
P
20

Why you're getting the error:

enter image description here

How to Fix it:

Surround the offending code with try catch - it's like saying, I will capture the error, log it and hopefully be able to do something about it.

 try {  // add this line
             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));   
                }
             }
         } catch (ParseException ex) {
              ex.printStackTrace(); // or log it using a logging framework
         }

or throw it at the main - here, it's like saying: whoever it is that's calling this method, please take care of this problem if it happens:

public static void main (String[] args) throws Exception
Pulliam answered 20/4, 2013 at 5:1 Comment(3)
Here's an answer that provides some explanation about why this error is happening, in the first place. :)Pulliam
That picture is really cool, what program did you use to make it?Aerate
While the other answer is straight to the point, yours is fun to read. Difference between a boring teacher and a fun teacher. Kudos!Psychopathy
H
13

Parse Exception is checked exception so you must have to handle it. Either by throws or try catch block.

public static void main (String[] args)

Should be

public static void main (String[] args) throws ParseException

Or in try catch block

try {
    //All your parse Operations
} catch (ParseException e) {
   //Handle exception here, most of the time you will just log it.
   e.printStackTrace();
  }
Heine answered 20/4, 2013 at 4:46 Comment(1)
@jmsutton334 I made some edit in my answer . If it works the way you wanted you can accept my answer.Heine

© 2022 - 2024 — McMap. All rights reserved.