Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I am trying to write a piece of code that not allow a duplicate item to be added to the list (and allow for either upper or lowercase names). But the same name keep adding onto the list. Can someone help me.

Java
public void findAndAddFlavor()
{
//Retrieve the new flavor
String inputFlavorString = inputTextField.getText();
	
//Search the items for a match
boolean foundFlavorBoolean = findDuplicate(inputFlavorString);
 if(foundFlavorBoolean)
    if (addBagelRadioButton.isSelected())
       JOptionPane.showMessageDialog(null,  "item is already in the list");
    else 
     {
      JOptionPane.showMessageDialog(null,  "item is already in the list");
      }
 else 
     if (addBagelRadioButton.isSelected())
     {
      bagelComboBox.addItem(inputFlavorString);
      JOptionPane.showMessageDialog(null,  "item added to the list");
     }
     else
     {
     cheeseComboBox.addItem(inputFlavorString);
     JOptionPane.showMessageDialog(null,  "item added to the list");
     }
}
	

public boolean findDuplicate(String inputString)
{
  boolean itemFoundBoolean = false;
  String listItemString = "";
  int indexInteger = 0;
  int indexInteger2 = 0;
  int numberBagelInteger = bagelComboBox.getItemCount(); //Gets number in combo box
  outputTextArea.append("\nTotal count in combo is: "  + numberBagelInteger); 
  int numberCreamCheeseInteger = cheeseComboBox.getItemCount(); //Gets number in combo box
  outputTextArea.append("\nTotal count in combo is: "  + numberCreamCheeseInteger);
		
     if (!itemFoundBoolean && indexInteger < numberBagelInteger)
       {
        {listItemString = String.valueOf(bagelComboBox.getItemAt(indexInteger));
	if(inputString.equalsIgnoreCase(listItemString))
	  {
	  itemFoundBoolean = true;
	  }
	  indexInteger++;
       }
     else if 
	(!itemFoundBoolean && indexInteger2 < numberCreamCheeseInteger)
       {
	listItemString = String.valueOf(cheeseComboBox.getItemAt(indexInteger2));
	   {
	     if(inputString.equalsIgnoreCase(listItemString))
		{
		itemFoundBoolean = true;
	        }
		indexInteger2++;
	    }
	}
      return itemFoundBoolean;
}
Posted

1 solution

Java
listItemString = String.valueOf(bagelComboBox.getItemAt(indexInteger));

Why do you use valueOf() here; your combo box items are already strings? A quick check with your debugger should tell you waht values are being returned.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900