Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching through an ArrayList of Strings to find text
    text
    copied!<p>This is what I have so far but I don't now what to do next. The question is as follows (sorry the coding is not all appearing in one box): Implement a method</p> <pre><code>public void search (String searchString) { } </code></pre> <p>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". When testing check for a String that is in the list and for one that isn't.</p> <p>Code:</p> <pre><code>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&lt;String&gt; notes; /** * Perform any initialization that is required for the * notebook. */ public Notebook() { notes = new ArrayList&lt;String&gt;(); } /** * 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 &lt; 0) { // This is not a valid note number, so do nothing. System.out.println("invalid index given"); } else if(noteNumber &lt; 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 than that"); // This is not a valid note number, so do nothing. } } public void removeNote(int noteNumber) { if(noteNumber &lt; 0) { // This is not a valid note number, so do nothing. System.out.println("invalid index given"); } else if(noteNumber &lt; numberOfNotes()) { // This is a valid note number. notes.remove(noteNumber); } else { System.out.println("there are fewer items in the notebook than that"); // This is not a valid note number, so do nothing. } } public void multiplesOfFive() { int i = 10; while(i &lt; 100) { System.out.println(i); i = i + 5; } } public int sum(int a, int b) { int index = a; int result = 0; while(index &lt;= b) { result = result + index; index = index + 1; } return result; } public int product(int a, int b) { int index = a; int result = 1; while(index &lt;= b) { result = result * index; index = index + 1; } return result; } public boolean isPrime (int n) { if (n&lt;=1)return false; if (n==2) return true; for (int i = 2;i&lt;=n-1;i++) { if (n%i==0)return false; } return true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload