我正在做一个大学项目,其中有多个键值。这是我的代码
import java.util.Hashtable;
private Hashtable name2email;
name2email = new Hashtable();
@Override
public void record_user(String name, String email, String address, String phonenumber, double accountnumber, double credit) throws user_exists {
if (name2email.containsKey(name)||
name2email.contains(email)||
name2email.contains(address)||
name2email.contains(phonenumber)||
name2email.contains(credit)||
name2email.contains(accountnumber))
{
throw new user_exists();
}
name2email.put(name, email,address,accountnumber,phonenumber,credit);
}
由于我的作业规范,我使用HashTable。有什么建议么?是的,这是我返回的错误
no suitable method found for put(String,String,String,double,String,double)
method Hashtable.put(Object,Object) is not applicable
(actual and formal argument lists differ in length)
我该怎么办 ?
最佳答案
哈希表适用于键值对。
清楚地
name2email.put(name, email,address,accountnumber,phonenumber,credit);
违反了Hashtable put语法。相反,您需要做这样的事情
name2email.put(name,new User(name,email,address,accountnumber,phonenumber,credit);
而且您的name2email定义应如下所示:
Hashtable<String,User> name2email= new Hashtable<String,User>();
检查记录是否存在,您需要执行以下操作:
User u = name2email.get(name);
if(u.getName().equals(name) || u.getEmail().equals(email)...(rest of your logic).
{ ...your user exists
}
else put(name,new User(name,email,address,accountnumber,phonenumber,credit);
关于java - 具有多个值的键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22249624/