从包含用户输入的CSV文件打印出行 -

时间:2013-09-18 23:23:24

标签: java csv io

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

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

  

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

     

乌鸦

     

对乌鸦的所有观察:Crow X Bergen 2015年5月

     

啄木鸟M奥斯陆2012年7月

     

Hummingbird M Kaupanger 2015年12月

而我只想打印出来:

  

Crow X Bergen 2015年5月

4 个答案:

答案 0 :(得分:0)

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

  1. boolean contains = bird.toLowerCase().contains(dataRow.toLowerCase());肯定是相反的方式(dataRow ... contains(... bird ...))
  2. 你永远不会重置布尔变量contains,以便它在第一次设置为true后打印所有内容......必须有contains = false某处
  3. 一般来说,你应该有一个看起来像这样的循环:

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

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

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

答案 1 :(得分: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
}

答案 2 :(得分:0)

  • 您的contains检查是错误的。格式为string.contains(substring)

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

  • contains = true应为contains == truecontainscontains = 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(); 
}

答案 3 :(得分: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分配给contains。你需要这样做:

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