[Bluej-discuss] Exercise 4.19 from the book....
Vivie
emor at it.teithe.gr
Fri Aug 11 21:44:28 BST 2006
Colleen Penrowley wrote:
>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
>>
>>
>
>
>_______________________________________________
>mailing list bluej-discuss at bluej.org
>To unsubscribe or change your preferences, go to
>http://lists.bluej.org/mailman/listinfo/bluej-discuss
>
>
>
Thank you so much Coleen..I did it and it worked!
Now I am stuck at the next exercise(4.20), I tried to do
public void listNotes()
{
for(String note : notes) {
System.out.println(noteNumber + ": " + note);
}
}
But I get an error..I can understand because noteNumber is a local
variable to showNotes only...
But I can't think of a workaround...
Should I make the noteNumber global? or something?
More information about the bluej-discuss
mailing list