Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否试图通过输入日期来查找数值?_Java_File_Arraylist_Compare_Contains - Fatal编程技术网

Java 是否试图通过输入日期来查找数值?

Java 是否试图通过输入日期来查找数值?,java,file,arraylist,compare,contains,Java,File,Arraylist,Compare,Contains,基本上,我是在尝试接受用户输入的日期,一旦搜索了日期,我想打印出它旁边的数字。问题是我的ArrayList不能在不输入整行而不仅仅是日期的情况下使用contain()方法进行搜索 public class SPX { public static void main(String[] args) throws IOException { ArrayList<String> dates = new ArrayList<>();

基本上,我是在尝试接受用户输入的日期,一旦搜索了日期,我想打印出它旁边的数字。问题是我的ArrayList不能在不输入整行而不仅仅是日期的情况下使用contain()方法进行搜索

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");
        }
    }
}

这些数字是文本文件的一部分,图片显示了它的格式。如果给定某个日期,我想打印出它旁边的值。我希望能得到一些关于如何实现这一目标的指导。任何帮助都将不胜感激。

您找不到任何东西的原因是您使用了
数组列表的
contains
-方法。如果整个字符串匹配,此方法仅返回
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());
  }
}

找不到任何内容的原因是使用了
ArrayList
contains
-方法。如果整个字符串匹配,此方法仅返回
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());
  }
}

您可以将数据读入
地图
,日期作为键,数字作为值。映射是一种数据类型,专门针对需要一起链接到键值关系中的值的情况。如何将第一个键与ArrayList中的值分开?我需要在空格之间分割吗?是的,按空格分割是正确的方法。请参阅:您只需将数据读入
地图
,日期为键,数字为值。映射是一种数据类型,专门针对需要一起链接到键值关系中的值的情况。如何将第一个键与ArrayList中的值分开?我需要在空格之间分割吗?是的,按空格分割是正确的方法。看:我甚至都没想过溪流!!很好,谢谢你!我会投票给你的,很高兴能帮上忙。不客气!我甚至都没想过溪流!!很好,谢谢你!我会投票给你的,很高兴能帮上忙。不客气!