Results 1 to 5 of 5
  1. #1
    Bas
    Bas is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default 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).

    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++;
               }
            }
        }      
      }
    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.
    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:
    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");
            }
    }
    This works fine whenever I search for a string which is in that arraylist.
    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++;
               }
            }
        }      
      }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    A typo in the last if statement:
    Java Code:
            if(found = false) {
    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:
        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");
            }
        }

  3. #3
    Bas
    Bas is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks. It works fine now.
    I now see that I made another mistake by typing this
    Java Code:
    if(it.next().contains (searchString)) {
                   if(found = true) {
                   System.out.println(searchString + " found");
               }
    in stead of
    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 don't understand how I could have made this mistake yesterday, I didn't mean to. :eek:


    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

  5. #5
    Bas
    Bas is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 08:38 PM
  2. Code for adding search function in an application
    By Avdhut in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-03-2009, 10:15 AM
  3. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  4. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM
  5. Replies: 2
    Last Post: 05-04-2008, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •