Parametrized object cannot be resolved to variable

Program125

I have this piece of code:

    @SuppressWarnings("unchecked")
    public void put(K key,V value){
        if(this.containsKey(key)){
            TableEntry<K,V> foundKey = (TableEntry<K,V>)this.getTableEntry(key);
            foundKey.setValue(value);
        } else{
            int slotNumber = Math.abs(key.hashCode()) % size;
            TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
        }

        // empty slot
        if(candidate == null){
            elements[slotNumber] = new TableEntry(key,value,null);
        }else{
            while(candidate != null){
                candidate = candidate.next;
            }
            candidate.next = new TableEntry(key,value,null);
        }
    }

Variables candidate and slotNumber are underlined in Eclipse, as are the invocations of the TableEntry() constructor. Can you tell me why I can't compare for example candidate with null?

If you need, here is complete class (Hash table):

package hr.fer.oop.lab3.prob2;

public class SimpleHashtable<K,V> {

    private V[] elements;
    private static int defaultsize = 16;
    private int size;

    public SimpleHashtable(){
        this(defaultsize);
    }

    @SuppressWarnings("unchecked")
    public SimpleHashtable(int initialCapacity){
        if(initialCapacity < 1) {
            throw new IllegalArgumentException("Capacity must be at least 1.");
        }
        elements = (V[])new Object[calculateCapacity(initialCapacity)];
    }

    public int calculateCapacity(int number){
        int result = 2;
        while(result < number){
            result = result << 1;
        }
        System.out.println(result);
        return result;
    }

    @SuppressWarnings("unchecked")
    public void put(K key,V value){
        if(this.containsKey(key)){
            TableEntry<K,V> foundKey = (TableEntry<K,V>)this.getTableEntry(key);
            foundKey.setValue(value);
        } else{
            int slotNumber = Math.abs(key.hashCode()) % size;
            TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
        }

        // empty slot
        if(candidate == null){
            elements[slotNumber] = new TableEntry(key,value,null);
        }else{
            while(candidate != null){
                candidate = candidate.next;
            }
            candidate.next = new TableEntry(key,value,null);
        }
    }

    @SuppressWarnings("unchecked")
    private TableEntry<K,V> getTableEntry(K key) {
        int slotNumber = Math.abs(key.hashCode()) % this.size;
        TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];

        while(candidate != null){
            if(key.equals(candidate.getKey())){
                return candidate;
            }
            candidate = candidate.next;
        }
        return null;
    }

    private boolean containsKey(K key) {
        return false;
    }

    private static class TableEntry<K,V>{
        K key;
        V value;
        TableEntry next = null;

        public TableEntry(K key, V value, TableEntry next){
            this.key = key;
            this.value = value;
            this.next = next;
        }

        K getKey(){
            return key;
        }

        V getValue(){
            return value;
        }

        void setValue(V value){
            this.value = value;
        }

        @Override
        public String toString(){
            return "Key:" + (String)key + "Value:" + (String)value;
        }
    }
}
John Bollinger

Your declarations of the candidate and slotNumber variables are visible only within the innermost block containing them, which in this case contains nothing else. It looks like you want to move the code that uses those variables into that block (as opposed to moving the declarations out of it):

    @SuppressWarnings("unchecked")
    public void put(K key, V value){
        if (this.containsKey(key)) {
            TableEntry<K, V> foundKey = this.getTableEntry(key);
            foundKey.setValue(value);
        } else {
            int slotNumber = Math.abs(key.hashCode()) % size;
            TableEntry<K,V> candidate = (TableEntry<K, V>) elements[slotNumber];

            // empty slot
            if (candidate == null) {
                elements[slotNumber] = new TableEntry<K, V>(key, value, null);
            } else {
                while (candidate != null) {
                    candidate = candidate.next;
                }
                candidate.next = new TableEntry<K, V>(key, value, null);
            }
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java - Object cannot be resolved to variable

From Dev

Java: object cannot be resolved to a variable

From Dev

AccessLevel cannot be resolved to a variable

From Dev

NimbusLookAndFeel cannot be resolved to a variable

From Dev

Class Cannot Be Resolved To A Variable

From Dev

R cannot be resolved to a variable --

From Dev

mStartTime cannot be resolved to a variable

From Dev

Matrix cannot be resolved to a variable

From Dev

“circle” Cannot be resolved to a variable

From Dev

timer cannot be resolved to a variable

From Dev

Android - Cannot Be Resolved to a Variable

From Dev

sqlq cannot be resolved to a variable

From Dev

_____ Cannot be resolved to a variable

From Dev

onClickListener cannot be resolved to a variable

From Dev

R cannot be resolved to a variable ---

From Dev

Index cannot be resolved to a variable

From Dev

LocalDateTime cannot be resolved to a variable

From Dev

cannot be resolved to variable

From Dev

DateSetListener object cannot be resolved

From Dev

Variable cannot be resolved in an If-Statement

From Dev

Return method cannot be resolved to variable

From Dev

cannot be resolved to a variable in a for loop in a loop

From Dev

Java - myVar cannot be resolved to a variable

From Dev

timepicker listener cannot be resolved to a variable

From Dev

TargetAPI - Build cannot be resolved to a variable

From Dev

R cannot resolved to a variable in android

From Dev

Java variable cannot be resolved or is not a field

From Dev

Picasso context cannot be resolved to a variable

From Dev

"Counter" cannot be resolved to a variable error?