错误在哪里?我想添加从散列中删除项目的功能 table
Where is the mistake? I wanted to add the ability to remove an item from a hash table
我创建了一个散列 table 并添加了添加新行的功能,而且,我想添加删除我创建的行的功能,但是 运行 出错了。
package com.company;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
Hashtable<Integer, String> TennisClub = new Hashtable<Integer, String>();
TennisClub.put(1, "Bob");
TennisClub.put(2, "Aaron");
TennisClub.put(3, "John");
TennisClub.put(4, "Alex");
TennisClub.put(5, "Kevin");
for(Integer itm: TennisClub.keySet()){
System.out.println(itm + " " + TennisClub.get(itm));
}
String name = "";
String way;
int id = 0;
Scanner in = new Scanner(System.in);
System.out.println("What do you want to do?");
way = in.next();
if (way.equals("add")){
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
System.out.println("Enter id:");
id = Integer.valueOf(in.next());
TennisClub.put(id, name);
}
System.out.println("\nHashtable:");
for(Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
if (way.equals("remove")) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
TennisClub.remove(id, name);
}
System.out.println("\nHashtable:");
for (Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
else{
return;
}
}
}
我创建了一个散列 table 并添加了添加新行的功能,而且,我想添加删除我创建的行的功能,但是 运行 出错了。
在您删除值的代码中,您没有获取输入名称的键。 id 变量的值是您的代码生成的最后一个值,而不是与名称关联的键。您需要先找到钥匙,然后再取出。
public class Test {
public static void main(String[] args) {
Hashtable<Integer, String> TennisClub = new Hashtable<Integer, String>();
TennisClub.put(1, "Bob");
TennisClub.put(2, "Aaron");
TennisClub.put(3, "John");
TennisClub.put(4, "Alex");
TennisClub.put(5, "Kevin");
for(Integer itm: TennisClub.keySet()){
System.out.println(itm + " " + TennisClub.get(itm));
}
String name = "";
String way;
int id = 0;
Scanner in = new Scanner(System.in);
System.out.println("What do you want to do?");
way = in.next();
if (way.equals("add")){
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
System.out.println("Enter id:");
id = Integer.valueOf(in.next());
TennisClub.put(id, name);
}
System.out.println("\nHashtable:");
for(Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
if (way.equals("remove")) {
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
// search for the id corresponding to input name.
for (Map.Entry<Integer, String> entry : TennisClub.entrySet()) {
if (entry.getValue().equals(name)) {
id = entry.getKey();
}
}
TennisClub.remove(id, name);
}
System.out.println("\nHashtable:");
for (Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
else{
return;
}
}
}
我创建了一个散列 table 并添加了添加新行的功能,而且,我想添加删除我创建的行的功能,但是 运行 出错了。
package com.company;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
Hashtable<Integer, String> TennisClub = new Hashtable<Integer, String>();
TennisClub.put(1, "Bob");
TennisClub.put(2, "Aaron");
TennisClub.put(3, "John");
TennisClub.put(4, "Alex");
TennisClub.put(5, "Kevin");
for(Integer itm: TennisClub.keySet()){
System.out.println(itm + " " + TennisClub.get(itm));
}
String name = "";
String way;
int id = 0;
Scanner in = new Scanner(System.in);
System.out.println("What do you want to do?");
way = in.next();
if (way.equals("add")){
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
System.out.println("Enter id:");
id = Integer.valueOf(in.next());
TennisClub.put(id, name);
}
System.out.println("\nHashtable:");
for(Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
if (way.equals("remove")) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
TennisClub.remove(id, name);
}
System.out.println("\nHashtable:");
for (Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
else{
return;
}
}
}
我创建了一个散列 table 并添加了添加新行的功能,而且,我想添加删除我创建的行的功能,但是 运行 出错了。
在您删除值的代码中,您没有获取输入名称的键。 id 变量的值是您的代码生成的最后一个值,而不是与名称关联的键。您需要先找到钥匙,然后再取出。
public class Test {
public static void main(String[] args) {
Hashtable<Integer, String> TennisClub = new Hashtable<Integer, String>();
TennisClub.put(1, "Bob");
TennisClub.put(2, "Aaron");
TennisClub.put(3, "John");
TennisClub.put(4, "Alex");
TennisClub.put(5, "Kevin");
for(Integer itm: TennisClub.keySet()){
System.out.println(itm + " " + TennisClub.get(itm));
}
String name = "";
String way;
int id = 0;
Scanner in = new Scanner(System.in);
System.out.println("What do you want to do?");
way = in.next();
if (way.equals("add")){
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
System.out.println("Enter id:");
id = Integer.valueOf(in.next());
TennisClub.put(id, name);
}
System.out.println("\nHashtable:");
for(Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
if (way.equals("remove")) {
while (true) {
System.out.println("Enter name:");
name = in.next();
if (name.equals("end")) {
break;
}
// search for the id corresponding to input name.
for (Map.Entry<Integer, String> entry : TennisClub.entrySet()) {
if (entry.getValue().equals(name)) {
id = entry.getKey();
}
}
TennisClub.remove(id, name);
}
System.out.println("\nHashtable:");
for (Integer n : TennisClub.keySet()) {
System.out.println("id:" + n + "\nName:" + TennisClub.get(n));
}
}
else{
return;
}
}
}