Java 搜索用户给定短语的对象数组列表
我的程序让用户将客户名称和客户电话号码添加到arraylist中。然后,如果用户需要,他们可以根据输入的内容在arraylist中搜索客户。如果用户输入nat名称,并且仅键入nat进行搜索。它应该显示包含nat的所有匹配项 我似乎不能正确地使用for循环,如果它正确的话。当我使用.equals方法时,它只匹配确切的内容 现在我已经更新了.equals to.contains。新的问题是 对于客户c:客户不工作。Customer是创建对象的类,customers是包含Customer对象的arraylist 地址:对于客户c: 错误消息显示: 错误:107,31 java:不兼容的类型:java.lang.Object无法转换为Customer> 方法,该方法将客户名称和电话号码添加到arraylist中Java 搜索用户给定短语的对象数组列表,java,string,for-loop,search,arraylist,Java,String,For Loop,Search,Arraylist,我的程序让用户将客户名称和客户电话号码添加到arraylist中。然后,如果用户需要,他们可以根据输入的内容在arraylist中搜索客户。如果用户输入nat名称,并且仅键入nat进行搜索。它应该显示包含nat的所有匹配项 我似乎不能正确地使用for循环,如果它正确的话。当我使用.equals方法时,它只匹配确切的内容 现在我已经更新了.equals to.contains。新的问题是 对于客户c:客户不工作。Customer是创建对象的类,customers是包含Customer对象的arra
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 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;
}
}
}
尝试将if条件更改为:
如果c.getCustomerName.containsfindName
java.lang.String.contains方法在且仅当
此字符串包含指定的字符值序列
此外,接受用户输入不需要两行:
字符串findName=keyboard.nextLine;
findName=keyboard.nextLine
删除第二行。ArrayList引用需要声明类型。 您在ArrayList对象创建中没有提到任何类型,因此它将客户作为类型对象 尝试如下替换主类
public class CustomerDirectory {
// Shorten Scanner
static Scanner keyboard = new Scanner(System.in);
static ArrayList<Customer> customers = new ArrayList<Customer>();
public static void main(String[] args) {
}
......
......
这一行很重要:static ArrayList customers=new ArrayList;请在问题中发布代码,而不是在链接中,而且绝对不能作为图像发布。链接有消亡的趋势,阅读问题的人无法执行图像。请原谅软包装导致的任何错误。寻求调试帮助的问题此代码为什么不起作用ng?必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。搜索在CS中是一个大主题,请更具体一些,发布一些代码并确定您的问题。我想您可能正在查找String.contains而不是String.equals。我已将其更改为.contains方法。关于这两行。当我只做了一行时,它没有等待扫描仪。它只是不断跳过它,我发现如果我放入第二行,它会暂停,等待用户输入名称。错误消息说它>找到“Customer.Required Object>在原始帖子中发布了更多详细信息。它是否说错误发生在哪里?对于客户c:是错误是。你能告诉我你在哪里创建客户阵列列表吗?这是根本问题…非常感谢。我书中的章节没有介绍这个。。。
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;
}
}
}
public class CustomerDirectory {
// Shorten Scanner
static Scanner keyboard = new Scanner(System.in);
static ArrayList<Customer> customers = new ArrayList<Customer>();
public static void main(String[] args) {
}
......
......