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

Thread: Array Population via .txt File

  1. #1
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Array Population via .txt File

    Alright, I've been plugging away at this code for quite some time. I finally felt it necessary to seek assistance in an online forum, as my JAVA mentor is away for the time being.

    My idea is to populate a String array, instantiated in the varlib class, with lines from a .txt document. This is the code of my method.

    try{
                BufferedReader br = new BufferedReader(new FileReader("db/tableA.txt"));
                for(int i=0; i<varlib.tableA.length; i++) {
                    varlib.tableA[i] = ("" + br.readLine());
                    System.out.println(varlib.tableA[i]);
                }
            }catch(IOException e) {
                System.err.println(e.getMessage());
            }

    I'm fairly new at the JAVA thing, but I've been doing some net searching... Not quite sure exactly what I am doing wrong here. Any help would be appreciated!
    Blackjack

    EDIT: I forgot to tell what it was doing. Basically, the println displays null for every value that there is in the txt file. For example, if the first line was the word Apple, the value of Table[0] is printed as Null
    Last edited by Blackrabbitjack; March 15th, 2012 at 10:43 PM. Reason: Inclusion of info


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

    Default Re: Array Population via .txt File

    When reading from a file, the readLine method will return null under some conditions.
    Read the API doc for the method to see when that happens.
    Your code should test the value returned by readLine to see if any data was returned before assuming you have data and can assign that data to the elements of an array.

    Basically do it this way:
    Read into a String, test if the String is null, if not null, save the String in the array.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Blackrabbitjack (March 16th, 2012)

  4. #3
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    The oracle document states that reader will return:
    A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

    That being said, I must be attempting to read the wrong line of the txt file, or have the file formatted wrong, because it has a population of 21 items. They're just words, with one word on each line. Is there some formatting error I'm making?

    Also, fixed up my code like you suggested.
    public void popTableAArray() {
            String readIn;
            try{
                BufferedReader br = new BufferedReader(new FileReader("db/tableA.txt"));
                for(int i=0; i<varlib.tableA.length; i++) {
                    readIn = br.readLine();
                    if(readIn != null) {
                        readIn = varlib.tableA[i];
                        System.out.println(varlib.tableA[i]);
                    }else{
                        System.out.println("File returning null value");
                    }
                }
            }catch(IOException e) {
                System.err.println(e.getMessage());
            }
        }

    When this is run, every time the loop is run, it returns the Else condition. Will continue to research, but if anyone has any ideas, I'm all ears.
    Blackjack

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    If this is printing: "File returning null value", then the file must be empty.
    Do a search to be sure there is only one file.

  6. #5
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    There is only one text file in the specified directory. It's formatted as such

    Sleeping
    Tired
    Hungry
    ....

    (those are the actual first three lines of the file) Is there something crucial I'm missing here? In varlib, the variable is created as such:
    public static String tableA[] = new String[100];
    Maybe my variable is created wrong, or I'm using the wrong type?
    Blackjack

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    Print out the value of readIn right after you read into it to see what was read.
    Print it out again after the assignment statement into the array.

    Does it print out the same number of lines as there are lines in the file?

  8. #7
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    This code:
    public void popTableAArray() {
            String readIn;
            try{
                BufferedReader br = new BufferedReader(new FileReader("db/tableA.txt"));
                for(int i=0; i<varlib.tableA.length; i++) {
                    readIn = br.readLine();
                    System.out.println(readIn);
                    if(readIn != null) {
                        readIn = varlib.tableA[i];
                        System.out.println(varlib.tableA[i]);
                    }else{
                        System.out.println("File returning null value");
                    }
                }
            }catch(IOException e) {
                System.err.println(e.getMessage());
            }
        }

    ...prints this in the terminal window:

    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    null
    File returning null value
    47 values alternating... -scratches head- Is there any more information I can provide that would help?
    Blackjack

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    Why are the lines printed out in different order than the program shows they should be?
    null should print before your message?

    You should add an id String with a variable when you print it to make for positive identification:
    System.out.println("readIn=" + readIn + "<");


    The computer says that the file it is reading is empty???
    Have you searched your disk to see where the empty file is?

  10. #9
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    You're asking the wrong guy. What are you talking about "where the null file is?" Would it not show a IO Error if it loaded a file that doesn't exist? My pathing is entirely correct. I'll try putting it in the root directory. You have all the pertinent code applying to this error, so I'm not sure what else I can give you... Like I said, I'm no wizard at programming, so I'll continue to poke around, but.. yeah. Not sure what else I can do.
    Blackjack

  11. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array Population via .txt File

    Empty file is different to a non-existent file.
    FileNotFoundException isn't thrown if the file is empty.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    Have you done a search on your PC for the file you are trying to read?
    Are there any other files with the same name? Your program is reading from an empty one.

    readIn = br.readLine();
    System.out.println(readIn); //<<<<<<<<<<This should print BEFORE your message
    but your posted messages doesn't show it printing first???

    Did you change the println as I suggested?
    System.out.println("readIn=" + readIn + "<");

    That would print something every time.

    What if the file had a single space?
    you would not notice when a single space is printed. With the ID string you would see it.

  13. #12
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    Placed the file in the root directory, and changed the println to show the values of readIn. Still outputs my Else first, and still shows the values of the txt file as null. There are no other files on this hard disk that are of the same name. And what do you mean by a single space? In the file name?
    Blackjack

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    When you execute a println with a single space it would be easy to miss.

    You code reads a file and prints out the lines in the file on my system.

    I get this message for a non-existent file:
    java.io.FileNotFoundException: TestCode11X.java (The system cannot find the file specified)
    Last edited by Norm; March 16th, 2012 at 03:07 PM.

  15. #14
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array Population via .txt File

    Post the code you've got at the moment, along with the new output please.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  16. #15
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    public void popTableAArray() {
            String readIn;
     
            try{
                BufferedReader br = new BufferedReader(new FileReader("tableA.txt"));
                for(int i=0; i<varlib.tableA.length; i++) {
                    readIn = br.readLine();
                    System.out.println("readIn=" + readIn + "<");
                    if(readIn != null) {
                        readIn = varlib.tableA[i];
                        System.out.println(varlib.tableA[i]);
                    }else{
                        System.out.println("File returning null value");
                    }
                }
            }catch(IOException e) {
                System.err.println(e.getMessage());
            }
        }

    This is my current code, with an output of:

    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value
    readIn=null<
    File returning null value

    I'll be away until Sunday afternoon, so don't think I've abandoned the forum, just won't have my computer with me.
    Blackjack

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    Your output does not make sense. Look at these lines of code:
      readIn = br.readLine();
      System.out.println("readIn=" + readIn + "<");  // This line should ALWAYS print first
    Look at the program's output. The above output is printed second

    For better debug output, add a println BEFORE the for loop to show when the output is going to be starting.

  18. #17
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    public void popTableAArray() {
            String readIn;
     
            try{
                BufferedReader br = new BufferedReader(new FileReader("tableA.txt"));
                System.out.println("For loop is about to run.");
                for(int i=0; i<varlib.tableA.length; i++) {
                    readIn = br.readLine();
                    System.out.println("readIn = " + readIn + " <");
                    if(readIn != null) {
                        readIn = varlib.tableA[i];
                        System.out.println(varlib.tableA[i]);
                    }else{
                        System.out.println("File returning null value");
                    }
                }
            }catch(IOException e) {
                System.err.println(e.getMessage());
            }
        }

    I may have realized that all of the display is not showing on the terminal window. I knocked down the array list to the amount of lines of text I have in my file, (10) and this is my return:

    For loop is about to run.
    readIn = Sleeping <
    null
    readIn = Tired <
    null
    readIn = Hungry <
    null
    readIn = Rough <
    null
    readIn = Rowdy <
    null
    readIn = Angelic <
    null
    readIn = Stoic <
    null
    readIn = Rusty <
    null
    readIn = Corroded <
    null
    readIn = Running <
    null

    It assigns the correct value to the variable, but when I try to assign it to the string array and print it, it balks. Sorry for the absence, and I guess BlueJ's terminal only holds X number of outputs... I'm not sure how I feel about that.
    Blackjack

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Population via .txt File

    when I try to assign it to the string array
    What does this statement do?
    readIn = varlib.tableA[i];
    Is that what you want to do?
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    Blackrabbitjack (March 19th, 2012)

  21. #19
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Array Population via .txt File

    varlib.tableA[i] = readIn;

    Stupid mistake, but the code works. Thanks for pointing me in the right direction!
    Blackjack

Similar Threads

  1. Building an array from .txt file.
    By brudley5 in forum Object Oriented Programming
    Replies: 3
    Last Post: December 4th, 2011, 09:14 PM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. Reading from a sequential file to an array
    By Xrrak in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: August 20th, 2011, 01:33 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Splitting File into Array
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 3rd, 2010, 11:48 AM