Search code examples
javajava.util.scannerhashtable

Where is the mistake? I wanted to add the ability to remove an item from a hash table


I created a hash table and added the ability to add new rows, but also, I wanted to add the ability to delete rows that I created, but ran into an error.

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;
        }
    }
}

I created a hash table and added the ability to add new rows, but also, I wanted to add the ability to delete rows that I created, but ran into an error.


Solution

  • In your code for removing values, you are not getting the key for the input name. The value for id variable is the last value that your code generated rather than the key associated with name. You need to find the key first, then remove.

    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;
            }
        }
    }