java - null object in HashTable -


when try map.get(k) index in hashtable returns null , wondering if csv hashtable code.

public static void main(string[] args) throws parseexception, ioexception {      bufferedreader br = new bufferedreader(new filereader("primes.csv"));     string line =  null;     hashtable<string, string> map = new hashtable<string, string>(100);      while((line = br.readline()) != null){         string str[] = line.split(",");          map.put(str[0], str[1]);     }     br.close();     system.out.println(map);      int k;      k = integer.parseint(joptionpane.showinputdialog(f,"enter integer k whee"             + " 3 < k < 1229: "));      while (k <= 3 || k >= 1229){        k = integer.parseint(joptionpane.showinputdialog(f,"sorry try again: "));     }      joptionpane.showmessagedialog(f, "the "+ k +"th prime number " + map.get(k));  }    

also csv file contains

1, 1 (next line)

2, 2 (next line)

3, 3 (next line)

4, 5 (next line)

5, 7 , on

you use string keys in map , want retrieve objects integer keys :

string str[] = line.split(","); ... map.put(str[0], str[1]); ... int k; ... map.get(k); 

it not possible.
these not equals in terms of equals().
so, use same type key in both cases.
example use string in both cases, change get() invocation in way :

map.get(string.valueof(k)); 

besides, hashtable thread safe class (not efficient too).
don't need feature in code sample. hashmap enough.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -