0

Hi all so I have a list that looks like this screenshot

Basically, I am trying to take in user input of a date, once searching for the date I want to print out the numerical digit that sits beside it. The problem is that my ArrayList can't search with the contain() method without entering in the whole line and not just the date.

public class SPX {

    public static void main(String[] args) throws IOException {
        ArrayList<String> dates = new ArrayList<>();
        
        BufferedReader read = null;
        
        try {
            read = new BufferedReader(new FileReader("C:\\Users\\rosha\\eclipse-workspace\\working\\src\\workingFix\\spx_data_five_years (1).txt"));
            String str;

            //reading to the ArrayList
            while ((str = read.readLine()) != null) {
                String[] lineValues = str.split(str, 1);
                dates.add(lineValues[0]);   
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //printing out the list
        for (String dList:dates) {
            System.out.println(dList);
        }
        findIndex(dates);   
    }
    public static void findIndex(ArrayList<String> dates) {
        Scanner sr = new Scanner(System.in);
        System.out.println("please enter a date: ");
        String userDate = sr.nextLine();
        sr.close();
        if (dates.contains(userDate)) {
            System.out.print("Spx Index: " + userDate);
        }
        else {
            System.out.print("Not found");
        }
    }
}
11/25/2014  2067.03
11/26/2014  2072.83
11/28/2014  2067.56
12/1/2014   2053.44
12/2/2014   2066.55
12/3/2014   2074.33
12/4/2014   2071.92
12/5/2014   2075.37
12/8/2014   2060.31
12/9/2014   2059.82
12/10/2014  2026.14

These numbers are a segment of the text file, the picture shows the format that it was in. If given a certain date, I want to print out the value next to it. Would love some guidance on how this could be accomplished. Any help would be greatly appreciated.

001
  • 13,291
  • 5
  • 35
  • 66
  • 1
    You could just read in the data into a `Map` with the date as key and the number as value. Maps are a data type specifically made for this case of needing to link to values in a key-value relationship together. – OH GOD SPIDERS Mar 18 '21 at 17:46
  • How could I separate the first key from the value within the ArrayList? Would I need to split between the white spaces? – Camacharia Mar 18 '21 at 17:57
  • Yes, splitting by whitespace would be the correct approach. See: [How to split a string with any whitespace chars as delimiters](https://stackoverflow.com/questions/225337/how-to-split-a-string-with-any-whitespace-chars-as-delimiters) – OH GOD SPIDERS Mar 18 '21 at 18:00

1 Answers1

2

The reason you dont find anything is because you use the contains-Method of ArrayList. This method returns only true if the whole String matches. If i understood you correctly, you want to search in each String of the List.

One possibility to do this is using streams. See the example below.

public static void findIndex(ArrayList<String> dates) {
  Scanner sr = new Scanner(System.in);
  System.out.println("please enter a date: ");
  String userDate = sr.nextLine();
  sr.close();

  String foundString = dates.stream().filter(date -> date.contains(userDate)).findFirst().orElse(null);

  if (foundString == null) {
    System.out.print("Not found");
  } else {
    System.out.print(foundString.substring(userDate.length()).trim());
  }
}

Another way is to use a Map<String, String> as mentioned in a comment to your question. Here the input is splitted by whitespace and the first section of the line is the key for the map, the second part is the value.

In the example code from your question, the split-Method you call is not necessary, you could also just add str to your ArrayList. Therefore i maybe your approach was leading to something like this:

public class SPX {

  public static void main(String[] args) throws IOException {
    Map<String, String> dates = new HashMap<>();

    BufferedReader read = null;

    try {
      read = new BufferedReader(new FileReader("C:\\Users\\rosha\\eclipse-workspace\\working\\src\\workingFix\\spx_data_five_years (1).txt"));
      String str;

      //reading to the ArrayList
      while ((str = read.readLine()) != null) {
        String[] lineValues = str.split(" ", 2);
        dates.put(lineValues[0], lineValues[1]);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (read != null) {
        try {
          read.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    //printing out the list
    for (String dList:dates.keySet()) {
      System.out.println(dList);
    }
    findIndex(dates);
  }

  public static void findIndex(Map<String, String> dates) {
    Scanner sr = new Scanner(System.in);
    System.out.println("please enter a date: ");
    String userDate = sr.nextLine();
    sr.close();

    String foundString = dates.get(userDate);

    if (foundString == null) {
      System.out.print("Not found");
    } else {
      System.out.print(foundString);
    }
  }
}
csalmhof
  • 1,820
  • 2
  • 15
  • 24