problem reading text file to hash map
So on my assignment I created a class to read a TXT file into a hashmap the txt contents is like this
1<>John Kane
2<>Jane Lyman
3<>Peter Hansen
4<>Cathy Harris
5<>Rose Makki
6<>Michael O'Connor
what i have so far
Code :
public class Reader
{
static Map<Integer, String> instructors, courses;
private static final String SEPARATOR = "<>";
public static Map<String, String> getInstructors()
{
try
{
BufferedReader in = new BufferedReader( new FileReader("instructor.txt"));
instructors = new LinkedHashMap<String, String>();
String line1;
while(((line1 = in.readLine()) != null))
{
line1 = in.readLine();
String[] val1 = line1.split(SEPARATOR);
String ID = val1[0];
String name = val1[1];
instructors.put(ID, name);
}
in.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
return instructors;
}
public static Map<String, String> getCourses()
{
try
{
BufferedReader in = new BufferedReader( new FileReader("course.txt"));
courses = new LinkedHashMap<String, String>();
String line2;
while(((line2 = in.readLine()) != null))
{
line2 = in.readLine();
String[] val2 = line2.split(SEPARATOR);
String ID = val2[0];
String name = val2[1];
courses.put(ID, name);
}
in.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
return courses;
}
}
I passed the instructor and course hashmap to my mainapp, but two problems occur when i try to display them onto a textarea with only the name
my instructor textarea appeared only 2, 6, and 4,(the actual name), and my courses throws an error refering to line 52 String[] val2 = line2.split(SEPARATOR); throwing an Exception in thread "main" java.lang.NullPointerException
can anyone help me out on this?
Re: problem reading text file to hash map
In both loops, you're reading a line, and checking to make sure it's not null. Then you're immediately reading another line, ignoring the line you read first, and not checking the newly read line for null. That's probably not what you want to be doing.
Re: problem reading text file to hash map