-5

I have a text file of ID numbers and dates in this format:

ID  Date
1   14/03/2011
2   06/03/2013
3   25/02/2013
4   28/01/2013
5   13/07/2011
6   13/03/2011
7   01/10/2012
8   09/10/2012

I need to read this txt file and store each date in a dictionary like a hashmap or something in the format. Also, it would be preferable if the dates are in the DATE data type and not String. I tried to do it myself but I am a beginner in JAVA and I couldn't get it done. Thanks in advance for any help

This is what I've tried till now. I haven't used a dictionary, I tried to put the dates in an ArrayList and for some reason, it reads only every alternate line from the file so half the dates are missing.

static Date[] list = new Date[59400];

public static void main(String[] args) {
    try {
        File file = new File("dates.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String readLine = "";

        int i =0;
        while ((readLine = bufferedReader.readLine()) != null) {
            String s = bufferedReader.readLine();
            Date date = new SimpleDateFormat("dd/MM/yyyy").parse(s);
            System.out.println(s+","+i);
            i++;
        }
        fileReader.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
Doctor
  • 35
  • 8
  • 2
    It'd be helpful you post what have you tried so far? – Pankaj Gadge Dec 16 '17 at 06:20
  • 1
    Welcome to StackOverflow. Please go through these two pages - [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – vinS Dec 16 '17 at 06:20
  • Search before posting. The issues in this Question have been addressed many many time already on Stack Overflow. – Basil Bourque Dec 16 '17 at 15:41

2 Answers2

1

.... for some reason, it reads only every alternate line from the file so half the dates are missing.

Look at these two lines from your code:

    while ((readLine = bufferedReader.readLine()) != null) {
        String s = bufferedReader.readLine();

First one reads a line of text into readLine.

Second one reads the next line into s.

The alternate lines that you read into readLine are never used.


Can I recommend that you take the time to read this article:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You can try the following function.

public static void parseFile() throws IOException, ParseException {
    BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("inputFileName.txt")));
    Map<Integer, Date> integerDateMap = new HashMap<>(); // Map to put data
    SimpleDateFormat sdfmt2= new SimpleDateFormat("dd/MM/yyyy"); // date format
    String line = null;
    line = bufferedReader.readLine(); // neglect first line
    while((line = bufferedReader.readLine())!= null){
         String[] split = line.split("   ");
         integerDateMap.put(Integer.parseInt(split[0]),sdfmt2.parse(split[1]));
    }
    System.out.println(integerDateMap); // print to check
}
Ran
  • 333
  • 1
  • 4
  • 16