Results 1 to 5 of 5
- 07-21-2009, 02:14 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
Simple search-function: what am I doing wrong?
The book with which I'm learning Java contains this code for searching (I only added the print-commands because that is an exercise in the book).
The problem with this code: when the found item is not the first in the arraylist you'll first get the sentence "... not found" for each item in the arraylist before that.Java Code:public void zoekWoord(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(searchString + " found"); } else { System.out.println(searchString + " not found"); index++; } } } }
So I tried to fix this so that when an object has been found the user will always only get the notice "... found" withoud first the notification "... not found" for eacht item which has a lower number in the arraylist.
My attempt to solve this:
This works fine whenever I search for a string which is in that arraylist.Java Code:public void zoekWoord(String searchString) { boolean found = false; Iterator <String> it = notes.iterator(); while(it.hasNext()) { if(it.next().contains (searchString)) { if(found = true) { System.out.println(searchString + " found"); } } } if(found = false) { System.out.println(searchString + " not found"); } }
Problem: when the boolean stays false (I checked it multiple times whit the debugger) the virtual machine (BlueJ) refuses to execute the last line.
Why doesn't this work?
To avoid any misunderstandings I'll add the entire code of that program from the book:
Java Code:import java.util.ArrayList; /** * A class to maintain an arbitrarily long list of notes. * Notes are numbered for external reference by a human user. * In this version, note numbers start at 0. * * @author David J. Barnes and Michael Kolling. * @version 2006.03.30 */ public class Notebook { // Storage for an arbitrary number of notes. private int getal; private ArrayList<String> notes; /** * Perform any initialization that is required for the * notebook. */ public Notebook() { notes = new ArrayList<String>(); } /** * Store a new note into the notebook. * @param note The note to be stored. */ public void storeNote(String note) { notes.add(note); } /** * @return The number of notes currently in the notebook. */ public int numberOfNotes() { return notes.size(); } /** * Show a note. * @param noteNumber The number of the note to be shown. */ public void showNote(int noteNumber) { // The &&-operator makes sure the 2nd operand is only // executed when the 1st operand evaluates to true if ((noteNumber >=1) && (noteNumber < numberOfNotes() + 1)) { // This is a valid note number, so we can print it. noteNumber = noteNumber - 1; System.out.println(notes.get(noteNumber)); } if ((noteNumber >=1) && (noteNumber > numberOfNotes())) { noteNumber = noteNumber - 1; System.out.println("ongeldig nummer"); } } public void removeNote(int noteNumber) { if ((noteNumber >=1) && (noteNumber < numberOfNotes() + 1)) { noteNumber = noteNumber - 1; notes.remove(noteNumber); } if ((noteNumber >=1) && (noteNumber > numberOfNotes())) { noteNumber = noteNumber - 1; System.out.println("ongeldig nummer"); } } public void listNotes() { getal = notes.size(); if (getal == 0) { System.out.println(" Er zijn geen notes."); } else { int index = 1; for(String note : notes) { System.out.println(index + " " + note); index ++; } } } public void zoekWoord(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(searchString + " found"); } else { System.out.println(searchString + " not found"); index++; } } } } }
- 07-21-2009, 06:38 AM #2
A typo in the last if statement:
The equality operator "==" causes the test statement to evaluate to a boolean. Using the assignment operator "=" assigns the value false to the variable found.Java Code:if(found = false) {
Java Code:public void zoekWoord(String searchString) { boolean found = false; Iterator <String> it = notes.iterator(); while(it.hasNext()) { if(it.next().contains (searchString)) { // set found to true so the last test will fail found = true; // we found it so report System.out.println(searchString + " found"); } } if(found == false) { System.out.println(searchString + " not found"); } }
- 07-22-2009, 04:43 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
Thanks. It works fine now.
I now see that I made another mistake by typing thisin stead ofJava Code:if(it.next().contains (searchString)) { if(found = true) { System.out.println(searchString + " found"); }I don't understand how I could have made this mistake yesterday, I didn't mean to. :eek:Java Code:if(it.next().contains (searchString)) { // set found to true so the last test will fail found = true; // we found it so report System.out.println(searchString + " found");
I understand the code but one thing confuses me. Why is it that a boolean is being set on false or true with one = (boolean ... = false) while you need == when you compare the boolean with a value.
I understand that in Java == has the same function as = has in math.Last edited by Bas; 07-22-2009 at 04:59 AM.
- 07-22-2009, 06:23 AM #4
Operators are summarized in the table at the top of this page: Operators.
Note the difference between the equality and the assignment operator blocks.
For the difference:
"=": On the next page Assignment, Arithmetic, and Unary Operators see the The Simple Assignment Operator paragraph at the top of the page.
"==": The next page Equality, Relational, and Conditional Operators first paragraph discusses the equality operator.
- 07-23-2009, 09:45 PM #5
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Search function for ArrayList?
By javanoobie in forum New To JavaReplies: 11Last Post: 04-17-2009, 08:38 PM -
Code for adding search function in an application
By Avdhut in forum Threads and SynchronizationReplies: 5Last Post: 03-03-2009, 10:15 AM -
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM -
Simple Addition Program Outputting Wrong Value
By carlodelmundo in forum New To JavaReplies: 4Last Post: 08-05-2008, 03:37 AM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks