[Bluej-discuss] Exercise 4.19 from the book....

Colleen Penrowley cpen at telus.net
Fri Aug 11 19:21:38 BST 2006


Hi,

The way you have written your code, the "not found" message will be printed 
for each item in the notebook that does not contain the search term. So if 
the matching note is in position 2, you will see "not found" for positions 
0 and 1, and then the note from position 2.

What you want to do is search through the entire notebook before printing 
the "not found" message. So you need it to come after the loop, not inside 
the loop.

Colleen


At 08:56 PM 8/11/2006 +0300, you wrote:
>Hello....
>
>I am studuying the book on my own and I have this question....
>
>in project Notebook1, I am trying to do exercise 4.19 , with the
>iteration...
>
>  I thought the program when we execute the search , would print only one
>string...either the note that was found , or that it wasn't....
>
>but I saw it prints them both!! what's wrong with my code?
>
>I include the code...
>
>public void search(String searchString)
>     {
>         int index = 0;
>         boolean found = false;
>         while(index < notes.size() && !found) {
>             String note = notes.get(index);
>             if(note.contains(searchString)) {
>                 found = true;
>                System.out.println(note);
>               }
>             else {
>                 System.out.println("Search term not found.");
>                 index++;
>             }
>         }
>     }
>
>_______________________________________________
>mailing list bluej-discuss at bluej.org
>To unsubscribe or change your preferences, go to
>http://lists.bluej.org/mailman/listinfo/bluej-discuss




More information about the bluej-discuss mailing list