adding characters to a collection
Hello, I've been working on this project where I have to add characters from and input string into a bag. I have the program and the bag collection working properly. I'm trying to adjust the add() method so it will only insert a character into the collection if no duplicate values exist, and return true. If there is a duplicate value, it should return false.
Here is my snippit of code for adding characters to "NodupBagA":
// add characters from the string to NodupBagA
for (int i = 0; i < str.length(); i++)
NodupBagA.add(str.charAt(i));
Here is my add() method in the NodupBag Class:
/**
* Inserts item in the NodupBag if space is available; that is,
* it the size is less than the capacity.
* Returns <tt>true</tt> if a new element is added and <tt>false</tt>
* if a duplicate value exists.
*
* @param item element that is added if space is available..
* @return <tt>true</tt> if a new element is inserted in the NodupBag.
*
*/
publicboolean add(T item)
{
boolean returnValue;
if (NodupBagSize >= NodupBagArr.length)
returnfalse;
else
{
// append item at index bagSize as long as no duplicates exist
NodupBagArr[NodupBagSize] = item;
// increment bagSize and return true
NodupBagSize++;
returntrue;
}
}
How would I get this to return false if a duplicate value is entered?

