javahashchained

Chained Hashing Program; the method is undefined for the type error


I have made multiple methods, and they all have the error called

The method add(int, ChainHashEx.Data) is undefined for the type ChainHash<Integer,ChainHashEx.Data>

and there is another problem that is

The constructor ChainHash<Integer,ChainHashEx.Data>(int) is undefined

Why are the methods undefined? Is there something wrong with the class?

Or is there something wrong in someplace I am ignorant about.

adding code and the class with methods underneath.

code ChainHashEx :

package week14;

import java.util.Scanner;

public class ChainHashEx {
    static Scanner sc = new Scanner(System.in);
    
    static class Data {
        private int no;
        private String name;
        
        public int keyCode()    {
            return no;
        }
        
        public String toString() {
            return name;
        }
        
        void scanData(String guide, int sw) {
            System.out.println(guide + "enter data to add");
            if (sw == 1) {
                System.out.print("number : ");
                no = sc.nextInt();
                System.out.print("name : ");
                name = sc.next(); 
            } else {
                System.out.print("number : ");
                no = sc.nextInt();
            }
        }
    }
    
    static  void printMenu() {
        System.out.println("1. add 2. delete 3. search 4. print 5. exit ");
    }
    
    public static void main(String[] args) {
        int menu;
        Data data;
        Data temp = new Data();
        
        ChainHash<Integer, Data> hash = new ChainHash<Integer, Data>(13);
         
        do {
            printMenu();
            System.out.print("select menu: ");
            switch(menu = sc.nextInt()) {
            case 1 :
                data = new Data();
                data.scanData("add", 1);
                hash.add(data.keyCode(), data);
                break;
            case 2 : 
                temp.scanData("delete", 2);
                hash.remove(temp.keyCode());
                break;
            case 3 : 
                temp.scanData("search", 2);
                Data t = hash.search(temp.keyCode());
                if (t != null)
                    System.out.println("searched : " + t);
                else
                    System.out.println("the data does not exist");
                break;
            case 4 : 
                hash.dump();
                break;          
            }           
        } while (menu != 5);
        
        System.out.println("stop program");
    }
}

class ChainHash :

package week14;


public class ChainHash<K,V> {
    class Node<K, V> {
        private K key;
        private V data;
        private Node<K, V> next;


        public Node(K key, V data, Node<K, V> next) {
            this.key = key;
            this.data = data; 
            this.next = next;
        }
        
        K getKey() {
            return key;
        }   
        
        V getValue() {
            return data;
        }
        
        private int size;
        private Node<K,V>[] table;
        
        public void ChainHash(int capacity) { 
            try {
                table = new Node[capacity];
                this.size = capacity; 
            } catch (OutOfMemoryError e) {
                this.size = 0;
            }
        }
        
        public int hashValue(Object key) {
            return key.hashCode() % size;       
        }
        
        public V search(K key)  {
            int hash = hashValue(key);
            Node<K,V> p = table[hash];
            
            while (p != null) {
                if (p.getKey().equals(key))
                    return p.getValue();
            
                p = p.next;
            }
        
            return null;    
        
        }
        
        public int add(K key, V data)   {
            int hash = hashValue(key);
            Node<K,V> p = table[hash];
            
            while (p != null) {
                if (p.getKey().equals(key))
                    return 1;
            
                p = p.next;
            }
        
            Node<K,V> temp = new Node<K,V>(key, data, table[hash]);
            table[hash] = temp;
            return 0; 
        }       
        
        public void dump() {
            for (int i=0; i<size; i++) {
                Node<K,V> p = table[i];
                System.out.printf("%02d ", i);
                
                while (p != null) {
                    System.out.printf("-> %s (%s) ", p.getKey(), p.getValue());
                    p = p.next;
                }
                
                System.out.println();
            }
        }
        
        public int remove(K key)    {
            int hash = hashValue(key);
            Node<K,V> p = table[hash];
            Node<K,V> pp = null;
            
            while (p != null) {
                if (p.getKey().equals(key)) {
                    if (pp == null)
                        table[hash] = p.next;
                    else 
                        pp.next = p.next;
                
                    return 0;
                }
            
                pp = p;
                p = p.next;
            }
            
            return 1;   
        }
    }   
}

Solution

  • your ChainHash methods are defined inside the Node class, that's why they can't be found.

    I think it's a "braces" error:

    after these fixes the code should compile.