【问题标题】:Printing out line from CSV file that contains user input -从 CSV 文件中打印出包含用户输入的行 -
【发布时间】:2013-09-18 23:23:24
【问题描述】:

我有一个程序允许用户将观察结果输入 CSV 文件并保存。 我希望能够读取文件并且只打印出用户搜索的观察结果。 (例如,用户输入“planet”并打印出所有包含 Planet 的行。我当前的代码打印出整个文件,而不仅仅是指定的行。我无法设置逻辑语句来执行此操作。

这是我的代码:

void findbird() throws IOException{


    Scanner input = new Scanner(System.in);
    System.out.println("Please enter the type of bird you wish to search for");
    String bird;
    bird = input.next();
    System.out.println("All observations of " + bird + "s:");

    BufferedReader br = new BufferedReader(new FileReader("birdobservations.txt"));
    String dataRow = br.readLine();
    boolean contains = bird.toLowerCase().contains(dataRow.toLowerCase());

    while (dataRow != null){
        String[] dataArray = dataRow.split(",");
        for (String item:dataArray) { 
            if(contains = true){
                System.out.print(item + "\t"); 
            }else{
                System.out.println("No observations of " + bird + " found!");
            }

        }

        System.out.println(); 
        dataRow = br.readLine(); 
    }

    br.close();
    System.out.println();

    menu();
}

我的输出目前如下所示:

请输入您要搜索的鸟类类型

乌鸦

Crows 的所有观察结果:Crow X Bergen May2015

啄木鸟 M 奥斯陆 2012 年 7 月

蜂鸟 M Kaupanger 2015 年 12 月

而我只想打印出来:

乌鸦 X 卑尔根 2015 年 5 月

【问题讨论】:

  • 不应该 if(contains = true)if(contains == true)
  • 糟糕,是的。我改变了它,但仍然是相同的输出。
  • 或者干脆if(contains)
  • 试过了,还是打印出同样的东西。

标签: java csv io


【解决方案1】:

有几个问题....最明显的两个是:

  1. boolean contains = bird.toLowerCase().contains(dataRow.toLowerCase()); 肯定应该反过来(dataRow...contains(...bird...))
  2. 您永远不会重置布尔变量 contains,以便它在第一次设置为 true 后打印所有内容...必须有一个 contains = false 某处

一般来说,你应该有一个看起来像这样的循环:

String dataRow = null;
while ( (dataRow = scanner.readLine() ) != null) {
   ....
}

这样你就不需要在循环外做愚蠢的 readLine 了,这会让你的代码很麻烦。

一旦您解决了这些代码问题,然后再次返回编辑问题。

【讨论】:

    【解决方案2】:

    这是问题代码

            String dataRow = br.readLine();
            boolean contains = bird.toLowerCase().contains(dataRow.toLowerCase());
    
        while (dataRow != null){
        String[] dataArray = dataRow.split(",");
            for (String item:dataArray) { 
                if(contains = true){
                    System.out.print(item + "\t"); 
                }else{
                    System.out.println("No observations of " + bird + " found!");
                }
     }
    

    改成

    while((dataRow = br.readline())!= null)
    {
    //And now you get value of contain and then check if contain == true and add other piece of code
    }
    

    【讨论】:

      【解决方案3】:
      • 您的contains 检查方法错误。格式为string.contains(substring)

      • contains 应该在循环内移动,否则你只需在开始时设置一次,而不是为每一行设置一次。

      • contains = true 应该是 contains == true 或简单的 containscontains = true 是赋值并且总是返回 true,不管 contains 有什么值。

      • contains检查移到for循环之外,否则它将为行中的每一列打印一条消息,而不仅仅是每行一次。

      • System.out.println(); 将导致打印空白行,如果这是不需要的(可能是),则应将其删除。

      代码:

      while (dataRow != null){
          boolean contains = dataRow.toLowerCase().contains(bird.toLowerCase());
          String[] dataArray = dataRow.split(",");
          if(contains){
              for (String item:dataArray) { 
                  System.out.print(item + "\t"); 
              }
          }
          else
          {
              System.out.println("No observations of " + bird + " found!");
          }
      
          dataRow = br.readLine(); 
      }
      

      【讨论】:

      • 谢谢,这绝对是我想要的!不过,我的输出中有很多空行,它们对应于文件中不匹配的行。有什么办法可以修剪吗?
      【解决方案4】:

      你的逻辑倒退了。做:

       boolean contains = dataRow.toLowerCase().contains(bird.toLowerCase());
      

      另外,你拥有的事实:

                  if(contains = true){
                      System.out.print(item + "\t"); 
                  }else{
                      System.out.println("No observations of " + bird + " found!");
                  }
      

      表示contains 将始终为真,因为您将true 分配给包含。你需要做的:

              if(contains == true){
                  System.out.print(item + "\t"); 
              }else{
                  System.out.println("No observations of " + bird + " found!");
              }
      

      【讨论】:

      • if (contains = true) 的行也有错误(应该是 ==(或 no = true),请考虑将其添加到您的答案中。
      • 谢谢!我不敢相信我错过了。
      猜你喜欢
      • 1970-01-01
      • 2019-10-16
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多