Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to get java program to add items to an unaccepted ArrayList
    primarykey
    data
    text
    <p>I've written a java picnic game (very basic), the only thing I don't know how to do is get the program not to accept items of the same letter. I also want the program to list how many times the user entered a rejected item. Note that this allows any ordering of items, as long as no two items start with the same letter (dis- regarding case). An acceptable sequence of inputs would be mustard , ketchup , tofu , anchovies . However, mustard , ketchup , tofu , and Kettle corn would not work since \ Kettle corn " begins with the same letter as \ ketchup</p> <h2>" (ignoring case).</h2> <pre><code>import java.util.*; public class PlayPicnic { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Picnic picnic = new Picnic(); ArrayList&lt;String&gt; unaccepted = new ArrayList&lt;&gt;();`enter code here` while (picnic.numberOfItems() &lt; 5) { System.out.print("What do you want to bring on the picnic? "); String item = scan.nextLine(); if (picnic.okayToBring(item)) { picnic.add(item); } else { if(!unaccepted.contains(item)) unaccepted.add(item); System.out.println("Sorry, you can't bring " + item); } } System.out.println("\nHere's what we'll have at the picnic:"); picnic.show(); System.out.println(Arrays.toString(unaccepted.toArray())); } } </code></pre> <hr> <h2>corresponding class</h2> <pre><code>import java.util.*; public class Picnic { // INSTANCE VARIABLES: private ArrayList&lt;String&gt; stuffToBring; // items to bring on the picnic // CONSTRUCTOR: //----------------------------------------------------- // Construct a new Picnic. //----------------------------------------------------- public Picnic() { stuffToBring = new ArrayList&lt;String&gt;(); // initialize list } //----------------------------------------------------- // Given an item s, see if it's okay to add it to the list. // Return true if it is, false otherwise: //----------------------------------------------------- public boolean okayToBring(String s) { // "Secret rule" -- s can't be an item already in the list: if (stuffToBring.contains(s)) // "contains" is in the ArrayList class { return false; } else { return true; } } //----------------------------------------------------- // Given an item s, add it to the list (if it's okay to add it) //----------------------------------------------------- public void add(String s) { if (okayToBring(s)) // this test keeps people from cheating! { stuffToBring.add(s); } } //----------------------------------------------------- // Print the items in the list //----------------------------------------------------- public void show() { for (int i = 0; i &lt; stuffToBring.size(); i++) { String s = stuffToBring.get(i); System.out.println(s); } } //----------------------------------------------------- // Returns the number of items in the list: //----------------------------------------------------- public int numberOfItems() { return stuffToBring.size(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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