开始进行Java编程 [英] beginning java programming

查看:61
本文介绍了开始进行Java编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我正在尝试编写一段不允许将重复项添加到列表中的代码(并允许使用大写或小写名称).但是,相同的名称不断添加到列表中.有人可以帮我吗?

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.

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;
}

推荐答案

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


为什么在这里使用valueOf()?您的组合框项目已经是字符串了?与调试器进行快速检查后,应该知道返回了哪些值.


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.


这篇关于开始进行Java编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆