您好我在尝试迭代并将字符串列表与单个字符串进行比较时遇到了问题。
我试图预先读取一个字符串,然后将它与一个属性列表的特定属性进行比较。
public class EmailSearcher {
Reader reader = new KeyboardReader();
public EmailSearcher(ArrayList<Contact> AdressBook){
System.out.println("Type in the desire email to search for");
String m = reader.read();
for(int i = 0; i<AdressBook.size();i++){
if(AdressBook.get(i).getEmails().contains(m)){
AdressBook.get(i).display();
}
}
}
}
基本上它会从键盘读取,“遍历”每个元素,获取特定属性(在本例中为电子邮件列表),然后将最近读取的字符串与列表的不同元素进行比较。如果匹配则会显示联系信息。
问题是,它不起作用,我不知道为什么会这样。
public class Contacto implements Serializable {
private static final long serialVersionUID = -2750016433717133486L;
public String nombre;
public String apellido;
public String apodo;
public ArrayList<String> numeros = new ArrayList<String>();
ArrayList<String> emails = new ArrayList<String>();
transient Reader reader = new KeyboardReader();
public Contacto (){
System.out.println("Nombre:");
setNombre();
System.out.println("Apellido:");
setApellido();
System.out.println("Apodo:");
setApodo();
AddNumber();
AddEmail();
}
public void AddEmail(){
boolean flag = true;
String str;
String varios= null;
String tipnum = null;
while(flag == true){
System.out.println("Write the type of Email.");
System.out.println("1.Personal 2.Job");
if((varios = reader.read()) == "Personal")
varios = "Personal";
else if(varios == "Trabajo")
varios = "Trabajo";
System.out.println(" To add Emails just type it in and press Enter");
if((str = reader.read()) !=null ){
tipnum = varios + " " + str;
emails.add(tipnum);
}
else if (str == null){
flag = false;
}
}
}
public ArrayList<String> getEmails(){
return (emails);
}
添加了联系人代码但缩短了。它太大了,所以我拿出了没有涉及这个主题的内容。