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 8 of 8

Thread: Reading from a txt file into 3 arraylists and save it as double

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading from a txt file into 3 arraylists and save it as double

    Here's my code so far, as I understand it it doesn't succeed reading in the numbers,

    I have a txt file which is tab delimited, in 3 columns and a lot of rows, e.g
    1 23 343
    2 34 355
    3 31 435
    ...
    etc.
    ...

    So, i want to read in every number from the txt file into 3 different Array lists.


     
     
    import java.util.ArrayList;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.BufferedReader;
    import java.io.IOException;
     
     
    public class dataIn {
     
        public void readFile()
        {
        String line = null;
     
        ArrayList[] chData = new ArrayList[3];
     
        try {
     
            FileReader fr = new FileReader("ch.txt");
            BufferedReader br = new BufferedReader(fr);
            while((line = br.readLine()) != null) {
     
     
     
                String[] theLine = line.split("\\t");
     
                chlData[0].add(Double.parseDouble(theLine[0]));
                chlData[1].add(Double.parseDouble(theLine[1]));
                chlData[2].add(Double.parseDouble(theLine[2]));
            }
     
        }
     
        catch(FileNotFoundException fN) {
            fN.printStackTrace();
        }
     
        catch(IOException e) {
            System.out.println(e);
        }
     
     
     
     
     
        }
     
    }

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    Can you explain what your problem is? Does the program compile ok and execute?
    If not, copy and paste here the full text of the error message.
    If it executes, does it do what you want?
    If not show what it does do and explain what you want different.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    Quote Originally Posted by Norm View Post
    Can you explain what your problem is? Does the program compile ok and execute?
    If not, copy and paste here the full text of the error message.
    If it executes, does it do what you want?
    If not show what it does do and explain what you want different.
    Hmm, i tried to reply but the message got lost. Trying again.

    The program compiles ok but the error occurs when i execute it. Here's the error messages.
    "run:
    Exception in thread "main" java.lang.NullPointerException
    at pocp.dataIn.readFile(dataIn.java:38)
    at pocp.PoCP.main(PoCP.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)"

    I want it to store the numbers in 3 different array lists. I have tried to print out the results from the different steps and as far as I understand it doesn't read in the numbers properly and gets stuck when it should read in the numbers for the first time in the first arraylist due to it's empty.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    What variable is null at line 38 in your program? When you see which one, back track to see why it has a null value.

    it doesn't read in the numbers properly
    Can you describe what happens?

    Print out the contents of the line variable to see what was read:
    System.out.println("line=" + line);

    Please wrap your posted code in code tags. Use the # icon in the Go Advanced button

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    Quote Originally Posted by Norm View Post
    What variable is null at line 38 in your program? When you see which one, back track to see why it has a null value.


    Can you describe what happens?

    Print out the contents of the line variable to see what was read:
    System.out.println("line=" + line);

    Please wrap your posted code in code tags. Use the # icon in the Go Advanced button

    Line 38 is

    chlData[0].add(Double.parseDouble(theLine[0]));

    It did write out the values from value "line" before line 38 at it actually snaps up the correct numbers. And when I print out the value "theLine[0]" before line 38 it also works.
    However, it gets stuck at line 38 so I gues the problem is somewhere in the add-function at line 38? Can you see something there which ain't right?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    What was the last line printed out before the error?
    For more info add this after the call to split()
    System.out.println("theLines=" + java.util.Arrays.toString(theLines));

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    Quote Originally Posted by Norm View Post
    What was the last line printed out before the error?
    For more info add this after the call to split()
    System.out.println("theLines=" + java.util.Arrays.toString(theLines));

    When i write it I get this message

    run:
    theLine=[1, 622803, 6.1]
    Exception in thread "main" java.lang.NullPointerException
    	at pocp.dataIn.readFile(dataIn.java:40)
    	at pocp.PoCP.main(PoCP.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    And the split seems to work, because when i write

               System.out.println(theLine[0]);
               System.out.println(theLine[1]);
               System.out.println(theLine[2]);


    I get this before the error

    run:
    1
    622803
    6.1
    Exception in thread "main" java.lang.NullPointerException
    	at pocp.dataIn.readFile(dataIn.java:42)
    	at pocp.PoCP.main(PoCP.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    And printing out ChlData before line 38 it shows null, which is as it should be.

    So the only two conclusions I can get is that the ArrayList is somehow not declarated properly, or that the parseDouble function isn't working.

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file into 3 arraylists and save it as double

    I have solved the problem now, I didn't declare the array lists properly.

    Thank you so much for all your help!

Similar Threads

  1. Reading from a file
    By NeedzABetterSN in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2011, 08:07 AM
  2. Save form as .pdf file?
    By Taron in forum AWT / Java Swing
    Replies: 3
    Last Post: November 14th, 2010, 02:20 PM
  3. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  4. [SOLVED] Executable .jar file isn’t launched after being double-clicked
    By voltaire in forum Java Theory & Questions
    Replies: 6
    Last Post: May 18th, 2010, 03:37 PM
  5. Saving .jsp page as .pdf file while generating report for struts based web application
    By ravindra_kumar_tiwari in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: August 12th, 2008, 09:32 AM