Hi all,

Im trying to remove from a HashTable by providing key and to get by proving key.
And i tried as follows for removing,
Caharcter ch=p.charAt(i);

but gives these errors.

myOwnBm.java:60: cannot find symbol
symbol : method Remove(char)
location: class java.util.Hashtable
myHash.Remove(p.charAt(i));
^
myOwnBm.java:77: inconvertible types
found : java.lang.Object
required: int
int v=(int)myHash.get(c);


this is my code for remove
Error in line 10

Hashtable myHash=new HashTable();
public  void preprocess(String p)
{

for (int i=0;i<p.length();i++)
{
	if (myHash.containsKey(p.charAt(i)))
	{
	
		myHash.Remove(p.charAt(i));
		myHash.put(p.charAt(i),i);
	}
	else
	{
		myHash.put(p.charAt(i),i);
	}

}
}

this is my code to get.
i tried
int value=(int)
Error in line 6

public int getLastOccurance(char c)
{

if (myHash.containsKey(c))
	{
	int v=(int)myHash.get(c);
	return v;
	
	}
	else
	{
	return -1;
	}
}

Edited 4 Years Ago by coroll: n/a

remove, not Remove - Java is case-sensitive.

HashTable takes only objects for key and value - your code relies on auto- boxing/unboxing to convert between char and Character, int and Integer. Your int cast on line 6 is probably interfering with the auto-unboxing

remove, not Remove - Java is case-sensitive.

HashTable takes only objects for key and value - your code relies on auto- boxing/unboxing to convert between char and Character, int and Integer. Your int cast on line 6 is probably interfering with the auto-unboxing

thank you.removing is ok.
BUT get matters.
i tried.
Character ch=Character(c);
int v=(int)myHash.get(ch);

still it says inconvertible types.

coucld you plz tech me how to convert char to Character

I think the error is is the (int) cast of the Object returned by the get(...) method, not in the char to Character conversion.

I think the error is is the (int) cast of the Object returned by the get(...) method, not in the char to Character conversion.

Oh Thanks i got it.

Integer v=(Integer)myHash.get(ch)

If you declare and create your Hashtable with the types, your code will be easier...
Hashtable<Character, Integer> myHash = new Hashtable<Character, Integer>();
now Java knows that the key needs to be converted to Character, and get will return an Integer, and you can assign that to an int and Java will convert automatically
int result = myHash.get('a');

This question has already been answered. Start a new discussion instead.