public class ReadToHashmap {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String>();
BufferedReader in = new BufferedReader(new FileReader("example.tab"));
String line = "";
while ((line = in.readLine()) != null) {
String parts[] = line.split("\t");
map.put(parts[0], parts[1]);
}
in.close();
System.out.println(map.toString());
}
}
while inserting the key with value in HashMapI am getting ArrayIndexOutOfBoundException exception on:-
map.put(parts[0], parts[1]);
partsarray before assuming it has at least two elements.ArrayIndexOutOfBoundExceptionisn't thrown by any method ofHashMapbut by the String arrayparts[]whose length in some case is less than 2.