Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class L10C
{

    public static void main(String[] args) throws Exception
    {
        File f = new File("src/Birthdates.txt");
        Scanner input = new Scanner(f);

        //-------------------------------------------------Read File & Create N2D Map
        Map<String, Date> n2d = new TreeMap<String, Date>();
        int n = input.nextInt();
        for (int r = 0; r < n; r++)
        {
            // String record = input.nextLine();
            // parse record into two pieces

            String name = input.next();
            String birthdateString = input.nextLine();
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
            Date birthdate = sdf.parse(birthdateString.trim());
            //System.out.println(name+" - " + birthdate);
            n2d.put(name, birthdate);
        }
        System.out.println("N2D: " + n2d); // debugging output

        //-------------------------------------------------Read File & Create D2N Map
        Map<Date, String> d2n = new TreeMap<Date, String>();
        for (String s : n2d.keySet())
        {
            Date d = n2d.get(s);
            if (!d2n.containsKey(d))
            {
                d2n.put(d, s);
            }
            else
            {
                String existingName = d2n.get(d);
                if (s.compareTo(existingName) == -1) // means s < existingName
                {
                    d2n.put(d, s);
                }
            }
        }
        System.out.println("D2N: " + d2n); // debugging outpu

        //-------------------------------------------------Output D2N Formatted
        for (Date d : d2n.keySet())
        {
            System.out.printf("%tb %<td, %<tY --> %s
", d, d2n.get(d));
        }
    }
}

Hi. I get

Exception in thread "main" java.text.ParseException: Unparseable date: "Jun 7, 1996"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at L10C.main(L10C.java:27)

error, my txt file has following inputs:

7 Randy Jun 7, 1996
Omar Feb 20, 1999
Sue Sep 14, 1990
Chris Sep 14, 1990
Adam Feb 20, 1996
Jim Sep 14, 1990
Phillip Oct 27, 1994

How can I fix error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are working on a Java 8 machine. For Java 8, Oralce offers new API to work with date and time. Please replace your current API (line 28) with the below API to make it works.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...