【问题标题】:Trying to find the numerical value by entering the dates?试图通过输入日期来查找数值?
【发布时间】:2021-06-16 03:54:56
【问题描述】:

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

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

【问题讨论】:

  • 您可以将数据读入Map&lt;String, String&gt;,其中日期为键,数字为值。 Maps 是专门为这种需要链接到键值关系中的值的情况而设计的数据类型。
  • 如何将第一个键与 ArrayList 中的值分开?我需要在空格之间拆分吗?
  • 是的,按空格分割是正确的方法。见:How to split a string with any whitespace chars as delimiters

标签: java file arraylist compare contains


【解决方案1】:

你没有找到任何东西的原因是因为你使用了contains-ArrayList的方法。如果整个字符串匹配,此方法仅返回 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&lt;String, String&gt;,如对您问题的评论中所述。这里输入被空格分割,第一部分是映射的键,第二部分是值。

在您问题的示例代码中,您调用的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);
    }
  }
}

【讨论】:

  • 我什至没有考虑流!效果很好,谢谢!我会支持你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多