2016-03-06 103 views
-4

我的程序有一个用户将一个客户姓名和客户电话号码添加到数组列表中。然后,如果用户希望他们可以根据用户输入的内容搜索(阵列列表),如果用户输入名称“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; 
    } 
} 
} 
+1

请在问题张贴代码,而不能链接,绝对不是为图像。链接有死亡倾向,阅读问题的人不能执行图像。 –

+0

请原谅软包装造成的任何错误。 –

+1

“寻求调试帮助的问题(”为什么这个代码不工作?“)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。“ –

回答

1

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<Customer> customers = new ArrayList<Customer>();

+0

这是根本问题....非常感谢。我书中的这一章没有包括这个... –

1

试着改变你的if条件:

if (c.getCustomerName().contains(findName))

The java.lang.String.contains() method returns true if and only if this string contains the specified sequence of char values.

而且,你并不需要2线接受用户输入:

String findName = keyboard.nextLine(); findName = keyboard.nextLine();

删除第二行。

+0

我已经改变它为.contains方法。关于两条线。当我做了一个。它wasn等待扫描仪,它只是不断跳过它,我发现如果我把它放在第二个,它暂停为用户输入一个名称。 –

+0

错误信息说它 >找到'客户。必需对象>发布更多详细信息在原来的职位。 –

+0

是否说错误发生在哪里? –