Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: problem reading text file to hash map

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: problem reading text file to hash map

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/56040-problem-reading-converting-file-hash-map.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. simple program for reading text file
    By johnpipes in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2011, 04:55 PM
  2. Reading a text file into an arraylist of fueldispensers.
    By vbhatti in forum Object Oriented Programming
    Replies: 3
    Last Post: November 20th, 2011, 12:17 PM
  3. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 04:31 PM
  4. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  5. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM