我的程序有一个用户将一个客户姓名和客户电话号码添加到数组列表中。然后,如果用户希望他们可以根据用户输入的内容搜索(阵列列表),如果用户输入名称“nate”,并且只输入nat进行搜索。它应该显示包含nat的所有匹配项。搜索给定词组的用户对象的数组列表
我似乎无法正确使用for循环,如果它。当我使用.equals方法时,它只匹配确切的内容。
现在我已将.equals更新为.contains。新问题是(客户c:客户)的 不起作用。客户是我创建对象的类,客户是包含客户对象的数组列表。
在:对(客户C: 错误消息说:
Error:(107, 31) java: incompatible types: java.lang.Object cannot be converted to Customer>
public class CustomerDirectory
{
//Shorten Scanner
static Scanner keyboard = new Scanner(System.in);
static ArrayList customers = new ArrayList();
public static void main(String[] args)
......
.......
方法在主类,增加了一个客户的姓名和电话号码到ArrayList
public static void addCustomer()
{
System.out.print("Enter new name: ");
String name = keyboard.nextLine();
name = keyboard.nextLine();
System.out.print("Enter new phone number: ");
String number = keyboard.nextLine();
Customer newCustomer = new Customer(name, number);
customers.add(newCustomer);
}
public static void findCustomer()
{
System.out.print("Enter name of customer to find: ");
String findName = keyboard.nextLine();
findName = keyboard.nextLine();
if (findName.equals(" "))
{
System.out.println("You did not enter a name.");
}
else
{
for (Customer c : customers)
{
if (c.getCustomerName().contains(findName))
{
System.out.println(c.getCustomerName());
}
}
}
}
创建要进入数组列表的对象的类。 class Customer { static String customerName; 串电话;
public Customer(String customerName, String telePhone)
{
setCustomerName(customerName);
setTelePhone(telePhone);
}
public static String getCustomerName()
{
return customerName;
}
public void setCustomerName(String customerName)
{
//Make the user input lowercase
customerName = customerName.toLowerCase();
if (customerName.equals(""))
{
System.out.println("You did not enter a name. Did not create new
customer.");
}
else if (customerName.length() > 25)
{
this.customerName = customerName.substring(0, 25);
}
else
{
this.customerName = customerName;
}
}
public String getTelePhone()
{
return telePhone;
}
public void setTelePhone(String telePhone)
{
if (telePhone.equals(""))
{
System.out.println("You did not enter a telephone number. Did not
create new customer.");
}
else
{
this.telePhone = telePhone;
}
}
}
请在问题张贴代码,而不能链接,绝对不是为图像。链接有死亡倾向,阅读问题的人不能执行图像。 –
请原谅软包装造成的任何错误。 –
“寻求调试帮助的问题(”为什么这个代码不工作?“)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。“ –