简体   繁体   中英

Printing out line from CSV file that contains user input -

I have a program that allows the user to input observations into a CSV file and save them. I want to be able to read the file and only print out observations that the user searches for. (ex. user types in "planet" and all lines containing planet are printed out. My current code prints out the whole file, and not just the specified lines. I'm having trouble setting up a logical statement to do this.

Here's my code:

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

My output currently looks like this:

Please enter the type of bird you wish to search for

Crow

All observations of Crows: Crow X Bergen May2015

Woodpecker M Oslo July2012

Hummingbird M Kaupanger December2015

Whereas I only want to print out:

Crow X Bergen May2015

There are a few problems.... the two most glaring are:

  1. boolean contains = bird.toLowerCase().contains(dataRow.toLowerCase()); should surely be the other way around (dataRow...contains(...bird...))
  2. You never reset the boolean variable contains so that it will print everything after it first is set to true... there has to be a contains = false somewhere

In general, you should probably have a loop that looks like:

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

That way you do not need to do the silly readLine outside the loop as well, which makes your code cumbersome.

Once you have fixed up these code issues then come back again with an edited question.

This is the problem code

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

Change it to

while((dataRow = br.readline())!= null)
{
//And now you get value of contain and then check if contain == true and add other piece of code
}
  • Your contains check is the wrong way around. The format is string.contains(substring) .

  • contains should be moved inside the loop, otherwise you just set it once in the beginning, instead of for each row.

  • contains = true should be contains == true or simply contains . contains = true is assignment and will always return true , regardless of the value contains had.

  • Move the contains check outside the for-loop, otherwise it will print a message for each column in the row, not just once per row.

  • System.out.println(); will cause blank lines to be printed, if this is unwanted (it probably is), it should be removed.

Code:

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

You have your logic backwards. Do:

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

Also, the fact that you have:

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

means that contains will always be true, because you are assigning true to contains. You need to do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM