如何解决试图通过输入日期来查找数值?
Hi all so I have a list that looks like this screenshot
基本上,我试图接受用户输入的日期,一旦搜索日期,我想打印出它旁边的数字。问题是我的 ArrayList 无法使用 contains() 方法进行搜索,而无需输入整行而不仅仅是日期。
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
这些数字是文本文件的一部分,图片显示了它的格式。如果给定某个日期,我想打印出它旁边的值。希望得到一些关于如何实现这一点的指导。任何帮助将不胜感激。
解决方法
您没有找到任何东西的原因是因为您使用了 contains
的 ArrayList
-Method。如果整个字符串匹配,此方法仅返回 true
。如果我理解正确,您想在列表的每个字符串中进行搜索。
这样做的一种可能性是使用流。请参阅下面的示例。
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());
}
}
另一种方法是使用在对您的问题的评论中提到的 Map<String,String>
。这里输入被空格分割,行的第一部分是地图的键,第二部分是值。
在您问题的示例代码中,您调用的 split
-Method 不是必需的,您也可以将 str
添加到您的 ArrayList。因此,我可能你的方法导致了这样的事情:
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);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。