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.