-4

I have a text file which looks like this

2017-06-14  7932
2017-06-15  10092
2017-06-16  7626
2017-06-17  7613
2017-06-18  11072
2017-06-19  8286
2017-06-20  9293

I am trying to store the values in an ArrayList.

My Code:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.*;
import java.util.Scanner;

public class Sample2 {

    public static void main(String[] args) throws FileNotFoundException {

        List<Date> l1 = new ArrayList<Date>();
        List<Integer> l2 = new ArrayList<Integer>();

        Scanner s = new Scanner(new FileReader("t1.txt"));

        while (s.hasNext()) {
         l1.add(s.nextInt());      !!!!-----Error Line-----!!!!!!!!
         l2.add(s.nextInt());
        }


        s.close();

        System.out.println(l1);
        System.out.println(l2);;
    }
}

I get the Following Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(int, Date) in the type List is not applicable for the arguments (int) at Sample2.main(Sample2.java:17)

How do I fix it?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Chid
  • 179
  • 2
  • 2
  • 12
  • You're trying to insert an int into a list of Dates. – J.N. Jul 26 '17 at 15:32
  • 1
    Side comment, if you use Java 8 you should use a `List` instead of a `List` – assylias Jul 26 '17 at 15:33
  • Do you know how to convert your input into a Date? – Mad Physicist Jul 26 '17 at 15:33
  • 1
    Possible duplicate of [read a date from a text file and put in a object date](https://stackoverflow.com/questions/8436600/read-a-date-from-a-text-file-and-put-in-a-object-date) – Mad Physicist Jul 26 '17 at 15:34
  • This is probably a better dupe: https://stackoverflow.com/q/30388616/2988730 – Mad Physicist Jul 26 '17 at 15:35
  • J.N, Is there a method that adds a date type to a list? Thanks – Chid Jul 26 '17 at 15:42
  • @MadPhysicist.. Is there a method that adds a date type to a list? Thanks – Chid Jul 26 '17 at 15:48
  • @Chid. `add` will do it for you, but you aren't adding a Date, you are trying to add an integer. – Mad Physicist Jul 26 '17 at 15:49
  • @Chid The method to add something to a list is `add`, but the problem in your code is that you declared a list of dates (`List`) and tried to add an `int` (`l1.add(s.nextInt())`). You must first parse the input `2017-06-14` to a date and then add it to the list. –  Jul 26 '17 at 15:50

3 Answers3

1

Two suggestions:

  1. Create a small class to hold the date and the int that belong together. Why? See Jon Skeet’s Anti-pattern: parallel collections.
  2. Skip the outdated Date class and use the modern LocalDate instead. It models a date without time-of-day, which is exactly what you need here. It also parses your dates right out of the box with no need for an explicit format, so it’s very convenient for your specific purpose.

Below I have just lumped the code together in one class, you will probably want a design with more classes. I will leave that to you.

public class Sample2 {

    private LocalDate date;
    private int value2;

    public Sample2(LocalDate date, int value2) {
        this.date = date;
        this.value2 = value2;
    }

    @Override
    public String toString() {
        return "\nSample2 [date=" + date + ", value2=" + value2 + "]";
    }

    public static void main(String[] args) throws FileNotFoundException {

        List<Sample2> l = new ArrayList<>();

        Scanner s = new Scanner(new FileReader("t1.txt"));

        while (s.hasNext()) {
            l.add(new Sample2(LocalDate.parse(s.next()), s.nextInt()));
        }

        s.close();

        System.out.println(l);
    }
}

The program prints

[
Sample2 [date=2017-06-14, value2=7932], 
Sample2 [date=2017-06-15, value2=10092], 
Sample2 [date=2017-06-16, value2=7626], 
Sample2 [date=2017-06-17, value2=7613], 
Sample2 [date=2017-06-18, value2=11072], 
Sample2 [date=2017-06-19, value2=8286], 
Sample2 [date=2017-06-20, value2=9293]]

I will also leave to you to add getters (you may not need setters) and to find better variable names. You know what the numbers mean.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Can you try with below code

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Sample2 {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

public static void main(String[] args) throws FileNotFoundException {

    List<Date> l1 = new ArrayList<Date>();
    List<Integer> l2 = new ArrayList<Integer>();

    Scanner s = new Scanner(new FileReader("t1.txt"));

    while (s.hasNext()) {
        l1.add(getDate(s.next()));
        l2.add(s.nextInt());
    }

    s.close();
    System.out.println(l1);
    System.out.println(l2);
}

private static Date getDate(String next) {
    Date date = null;
    try {
        date = dateFormat.parse(next);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

}

  • Better formatting and a word of explanation would be nice, but you have the right idea. Why is there an unformatted trailing brace? – Mad Physicist Jul 26 '17 at 15:49
  • @Ramachandra Reddy, Thanks. but the list is stored in the "Wed Jun 14 00:00:00 EDT 2017" format instead of yyyy-MM-DD. – Chid Jul 26 '17 at 15:57
  • Yes. The date will be stored in that format only. If you want to retain the "yyyy-MM-DD" format, you have to declare list l1 as List instead of List – Ramachandra Reddy Jul 26 '17 at 16:00
  • 1
    @Chid That's the result of [`Date.toString()` method](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--). Actually, a [date has no format](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/) –  Jul 26 '17 at 16:01
  • 1
    While parsing the dates into something that is meant for holding dates is a good idea, please don’t teach the young ones the long outdated classes `Date`, `SimpleDateFormat` and friends. Today we have so much better: I recommend the modern Java date and time API known as `java.time` or JSR-310. Start with the [Oracle tutorial](https://docs.oracle.com/javase/tutorial/datetime/). Then use `LocalDate`. Its `parse` method even parses the dates in the file without any explicit formatter. – Ole V.V. Jul 26 '17 at 18:36
0

You need to check for an int by using the nextInt() method.

"else" s.next() will return a String.

This will fix your error:

while (s.hasNext()) {
   if (s.hasNextInt()) {
       int myInt = s.nextInt();
       System.out.println(myInt);
   } else {
       String dateAsString = s.next();
       System.out.println(dateAsString);
   }
}

When I run this code, I get:

2017-06-14
7932
2017-06-15
10092
2017-06-16
7626
2017-06-17
7613
2017-06-18
11072
2017-06-19
8286
2017-06-20
9293

You will have to convert your date string to a localDate() or Date()

chocksaway
  • 870
  • 1
  • 10
  • 21