I want to implement my own hash table using chaining and linked list but I am having a hard time figuring out how to use the implementation in the main method. I need to read a comma separated values file with data and store the names as keys and the two floating points as a value. I know I need to use object oriented programming but I having a difficult time accessing my data using my implementation.
Here is my code: public class LinkedListHash{
String key;
String value;
LinkedListHash next;
public LinkedListHash(){
}
LinkedListHash(String key, String value){
this.key = key;
this.value = value;
this.next = null;
}
public String getValue(){
return value;
}
public void setValue(String value){
this.value = value;
}
public String getKey(){
return key;
}
public LinkedListHash getNext(){
return next;
}
public void setNext(LinkedListHash next){
this.next = next;
}
class Hashtable {
int size = 0;
LinkedListHash[] table;
Hashtable(){
table = new LinkedListHash[size];
for (int i = 0; i < size; i++){
table[i] = null;
}
}
public String get(String key){
int hash = key.hashCode();
if (table[hash] == null){
return null;
}
else {
LinkedListHash input = table[hash];
while (input != null && input.getKey() != key){
input = input.getNext();
}
if (input == null){
return null;
}
else {
return input.getValue();
}
}
}
public void put(String key, String value){
int hash = key.hashCode();
if (table[hash] == null){
table[hash] = new LinkedListHash(key, value);
}
else {
LinkedListHash input = table[hash];
while (input.getNext() != null && input.getKey() != key){
input = input.getNext();
}
if (input.getKey() == key){
input.setValue(value);
}
else {
input.setNext(new LinkedListHash(key, value));
}
}
}
}
}
public static void main(String[] args) throws FileNotFoundException{
Hashtable<String, String> tbl = new Hashtable<String, String>();
String path = args[0];
if(args.length < 1) {
System.out.println("Error, usage: java ClassName inputfile");
System.exit(1);
}
Scanner reader = new Scanner(new FileInputStream(args[0]));
while((path = reader.nextLine()) != null){
String parts[] = path.split("\t");
tbl.put(parts[0], parts[1]);
} reader.close();
} }
Any way I could improve my code would be helpful. Keep in mind I am not a very experienced programmer so I apologize for any horrendous mistakes.