so i’ve worked on hours end to come up with this code
public class instructorIO
{
static Map<String, String> instructors;
public static Map<String, String> getInstructors()
{
try
{
BufferedReader in = new BufferedReader( new FileReader("instructor.txt"));
instructors = new LinkedHashMap<String, String>();
String line;
while(((line = in.readLine()) != null))
{
line = in.readLine();
String[] val = line.split("<>");
String ID = val[0];
String name = val[1];
instructors.put(ID, name);
}
in.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
return instructors;
}
}
when i try to display all the hashmap values in my text area, only hash IDs 2,6, and 4 are displayed. There are a total of 6… what am i doing wrong?
also when i try to do that with another text file, i get a Exception in thread “main” java.lang.NullPointerException
at String[] val = line.split(“<>”);
You are reading two lines at a time and only taking the second one:
once in the
whilecondition and again inside the loop body. I suggest you do it like this: