-2

getting an array index out of a bound exception in below code. I have tried to put the error line inside try-catch, putting try catch also throwing an error in eclipse. can anyone pls tell me what I am doing wrong.

Error:

"Multiple markers at this line
- Syntax error on token "try", invalid 
 Modifiers
- Syntax error, insert "}" to complete 
 Block"

With try catch:

try {
    public hashMapimpl() {
        table =new Entry[5];
    }}catch(Exception e)
  {
        e.getMessage();
  }

Complete code:

import java.util.Map.Entry;
class hashMapimpl<K,V>  {   
    static int capacity=5;
    private Entry<K,V> [] table;

    static class Entry<K, V>{
        K key;
        V value;
        Entry<K,V> next;

        public Entry(K key,V value, Entry<K,V> next)
            {
            this.key=key;
            this.value=value;
            this.next=next;
        }

    }

    public hashMapimpl() {
        table =new Entry[capacity];
    }

    public void put(K newKey, V data) {
        if(newKey==null) {
            return;
        }
        int hash=hash(newKey);
        Entry<K,V> newentry = new Entry<K,V>(newKey,data,null);
        if(table[hash]==null)
            table[hash]=newentry;
        else {
            Entry<K,V> previous =null;

            Entry<K,V> current=table[hash];     
        while(current!=null)
        {
            if(current.key.equals(newKey))
            {
                if(previous==null) {
                    newentry.next=current.next;
                    table[hash]=newentry;
                    return;}
                else {
                    newentry.next=current.next;
                    table[hash]=newentry;
                    return;
                }
            }
            previous=current;
            current=current.next;
        }
        previous.next=newentry;
        }
    }


    public V get(K key) {
        int hash=hash(key);
        if(table[hash]==null) {
            return null;
        }
        else {
            Entry<K,V> temp=table[hash];
            while(temp!=null)
            {
                if(temp.key.equals(key))
                    temp=temp.next;
                    return temp.value;

            }
            return null;
        }
    }

    public void display()
    {

        for(int i=0;i<capacity;i++) {
            if(table[i]!=null) {
                Entry<K,V> e=table[i];
                while(e!=null)
                {
                    System.out.println(e.key);
                    System.out.println(e.value);
                    e=e.next;
                }
            }
        }
    }
    private int hash(K Key) {
        return Math.abs(Key.hashCode());
    }

public static class hashmap_main{
    public static void main(String[] args)
    {
        hashMapimpl<Integer,Integer> h1 = new hashMapimpl<Integer,Integer>();
        h1.put(2, 4);
        h1.put(4, 0);
        h1.put(23, 9);
        h1.put(1, 8);

        System.out.println(h1.get(23));

    }
}
}
John Kugelman
  • 307,513
  • 65
  • 473
  • 519

2 Answers2

2

The syntax for a try / catch is like this:

try {
    // statements

} catch(Exception e) {
    // more statements
}

Your code appears to have a constructor declaration

public hashMapimpl() {
    table = new Entry[5];
}

where there should be statements. That's not valid Java. And it makes little sense to me.

Either way, the Java compiler is rightly telling you that the syntax is wrong ... albeit without explaining why1.

It is possible that you mean this:

public hashMapimpl() {
    try {
        table = new Entry[capacity];
    } catch(Exception e) {
        System.err.println(e.getMessage());
    } 
}

but that's a bad idea too.

Q: What is going to happen if you get an exception?

A: The constructor is going to print an error message ... and then continue as if nothing had gone wrong. And that is going to leave you with a null in the table field ... which will most likely trigger NPEs a bit later.

The correct way to solve this is to figure out why you are getting an ArrayIndexOutOfBounds exception, and fix the cause of the exception.

Hint: what is the actual value of capacity when you create the array??


1 - Unfortunately, a compiler can only produce a meaningful error message if it understood what you meant by that. Compilers typically can't figure out what you (probably) mean if the syntax is too messed up.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • you are right i had checked already the capacity value i already declared capacity =4 and adding only four keys to array ,, it is under the bound but still i am getting ...that why is there any leak in the code ? – pankaj malvi Jan 21 '19 at 04:40
  • do you know how to check the internal impl of hashmap from eclipse ? – pankaj malvi Jan 21 '19 at 05:34
  • I don't understand what you are asking. – Stephen C Jan 21 '19 at 05:37
  • I mean to say can I find all the get, put ..etc methods inside java.util.hashmap package,to check internal working of hashmap – pankaj malvi Jan 22 '19 at 05:54
  • It is in the source code. You can find it by googling; e.g. for "java.util.HashMap source code". Or you can clone an entire OpenJDK source code tree from https://openjdk.java.net/ – Stephen C Jan 22 '19 at 06:34
-1

You can't have arrays of generic classes. Java simply doesn't support it. See this answer for using collections as a alternative.

https://stackoverflow.com/a/7131673/7260643

KingMathCS
  • 19
  • 4