0

我有一个程序,允许用户将观察结果输入 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();
}

我的输出目前看起来像这样:

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

乌鸦

乌鸦的所有观察:乌鸦 X 卑尔根 May2015

啄木鸟 M 奥斯陆 2012 年 7 月

蜂鸟 M Kaupanger 2015 年 12 月

而我只想打印出来:

乌鸦 X 卑尔根 2015 年 5 月

4

4 回答 4

0

这是问题代码

        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
}
于 2013-09-18T23:45:26.297 回答
0
  • 你的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(); 
}
于 2013-09-18T23:45:41.277 回答
0

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

  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,这会使您的代码变得繁琐。

一旦你修复了这些代码问题,然后再次带着一个编辑过的问题回来。

于 2013-09-18T23:43:43.803 回答
0

你有你的逻辑倒退。做:

 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!");
        }
于 2013-09-18T23:47:30.580 回答