tags:

views:

196

answers:

6

Implement a method

public void search (String searchString) { }

to iterate through the notes ArrayList until it finds a note that contains the searchString. It should then print either the item found or the message "String not found".

So far, I have:

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 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 2008.03.30
 */
public class Notebook
{
    // Storage for an arbitrary number of notes.
    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)
    {
        if(noteNumber < 0) {
            // This is not a valid note number, so do nothing.
            System.out.println("invalid index given");
        }
        else if(noteNumber < numberOfNotes()) {
            // This is a valid note number, so we can print it.
            System.out.println(notes.get(noteNumber));
        }
        else {
             System.out.println("there are fewer items in the notebook");
            // This is not a valid note number, so do nothing.
        }
    }

    public void removeNote(int noteNumber)
    {
        if(noteNumber < 0) {
            // This is not a valid note number, so do nothing.
             System.out.println("invalid index given");
        }
        else if(noteNumber < numberOfNotes()) {
            // This is a valid note number.
            notes.remove(noteNumber);
        }
        else {
            System.out.println("there are fewer items in the notebook");
            // This is not a valid note number, so do nothing.
        }
    }

    /* Edit note.
     * I tried to improve the formatting of the code below, but I'm completely
     * unable to figure out how on earth anything of that should make sense
     * and therefore the indentation is completely without any meaning.
     */
    public void search (String searchString)
    { 
        for each notes in ArrayList {    
            if notes = searchString;        
                System.out.println("String found"); + searchString        
                return    end 
            }
        if}
        System.out.println("String not found");
    }
}

But it is not working, and I am not able to work it out.
I would be grateful if someone would help me. Thank you.

+3  A: 

To iterate over the array list, you can use a 'for-each' loop:

for (String note: notes) {
    // Do something with note
}

This is very basic syntax. Have you seen it before? If not, you should start by reading a very basic tutorial to Java before attempting this homework.

Mark Byers
Since this is Java I'm not sure that would help.
Andrey
I didn't notice the generic ArrayList<T> at first, and thought it was C#. ArrayList is not generic in C# - the generic version is just called List<T>.
Mark Byers
+5  A: 

Several problems:

  1. Your search method is actually outside the class.
  2. The body of your search method makes no sense at all.

If you're counting on the monkeys writing Shakespeare, you'll be waiting a while.

Anon.
+2  A: 

Basically you want to loop over the elements, and for each one check whether it equals the element you are searching for. You can use a for loop or a foreach loop to do the actual iteration.

Andrey
+3  A: 

Fundamentally you need to look at each item in your ArrayList and test to see if it matches the search condition. In Pseudocode

for each note in notes
{
    if note equals searchString then 
        print "Found " + searchString
        return
    end if
}

print "not found"

Given that basic outline, want to take a second stab at coding it in Java?

Eric J.
+2  A: 

should you be checking if the whole note matches your searchstring, or if the note contains your searchstring?

i.e. given notes "foobar","baz","spam", should a search on "foo" return "foobar" or not match on anything?

so in pseudocode:

for each note in notes
{
     if searchstring in note
     {
           print "Found :"+note
     }
}
Steven Adams
+1  A: 

check this website http://pleac.sourceforge.net/pleac%5Fjava/arrays.html it may be useful

john milko