Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Norm

Thread: Need help reading text from files

  1. #1
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Need help reading text from files

    I'm using java to write this code

    I have a text file that has the following strings:
    John
    chris
    Zonado
    Buggy

    The output when I run this is:
    run:
    null null null null BUILD SUCCESSFUL (total time: 0 seconds)

    What is causing these null outputs? My array has the right amount of size to store the 4 strings.

    CROSS POST LINK HERE: http://www.coderanch.com/t/658987/ja...ing-text-files


    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package experiment;
    
    import java.util.Scanner;   // Needed for the Scanner class
    import java.io.*;           // Needed for the File and IOException
    
    /**
     *
     * @author
     */
    public class Experiment {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            // TODO code application logic here
            File myFile = new File("/Users/regies/NetBeansProjects/Sanchez_Project#2/ListOfNames.txt");
            Scanner inputFile = new Scanner(myFile);
            String[] names = new String[4];
            int index = 0;
    
            // save the names in an array
            while ((inputFile.hasNext()) && (index < names.length)) {
                System.out.println("Hello World");
                String currentName = inputFile.nextLine();
                names[index] = currentName;
                index++;
            }
    
            // print the names
            for (int i = 0; i < names.length; i++) {
                System.out.print(names[i] + " ");
            }
    
            // close the file 
            inputFile.close();
    
        } // end of main
    
    } // end of class Experiment
    Last edited by asilvester635; 12-13-2015 at 01:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Need help reading text from files

    Is the while loop executing? I don't see any print out from line 30.
    Try debugging the code by printing out the values of the expressions used to control the while loop to see if the computer thinks if they are true.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    I put a print statement System.out.println("Hello World"); inside the while loop and it did not print. You are right, the while loop is not executing, although I don't know why. Even If I remove the other conditional statement of the while loop, it still would not execute.

    I'm pretty sure that the program has access to my text file, if it didn't it would be giving me a file not found exception. So it's no that.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Need help reading text from files

    Is the file it is trying to read empty? Try printing out the value returned by the File class's length() method.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    No it is not empty. It contains strings in the format below.

    John
    chris
    Zonado
    Buggy

    To print the value returned by the File class's length() method I wrote...
    Java Code:
    System.out.println("Printing out the length of the file");
    System.out.println(myFile.length());
    The output was:
    Printing out the length of the file
    23469
    null null null null BUILD SUCCESSFUL (total time: 0 seconds)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Need help reading text from files

    That's a huge file for those 4 Strings. Can you look at the file in a hex editor to see what is in it?

    What happens if you comment out the hasNext() test in the while statement?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    If I comment out the hasNext() test in the while statement I get this result

    Printing out the length of the file
    23469
    Hello World
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at experiment.Experiment.main(Experiment.java:33)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by asilvester635; 12-13-2015 at 02:09 PM.

  8. #8
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    For my text file I used microsoft word and typed in the strings. I saved it normally. Once saved on my desktop I proceeded to add .txt at the end of the name. Is that how you save the text files for these? Maybe that's causing the problem?

  9. #9
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    The error message I got was wrong, I ran the program while the text file was not in the the proper folder. I ran it again and got the updated error message in post #7
    Here it is so you don't have to scroll up.

    Printing out the length of the file
    23469
    Hello World
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at experiment.Experiment.main(Experiment.java:33)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Need help reading text from files

    What is in that file?
    used microsoft word and typed in the strings. I saved it normally. Once saved on my desktop I proceeded to add .txt at the end of the name.
    I missed that. The file is a MS Word file, NOT a .txt file. You can NOT change the contents of a file by changing its extension.
    Create the file again and save it as a text file.
    Last edited by Norm; 12-13-2015 at 02:16 PM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    It contains strings below
    John
    chris
    Zonado
    Buggy

    I'm suspecting that the way I created the text file is the one that's cousin the problem.

    Okay I've created another file (not using microsoft word). I used my mac's built in text editor. When I ran the program again, the print statement in the while loop executed 4 times, but now there's a bunch of weird words. Here is the output. Though it still did not print the string from the text files.

    run:
    Printing out the length of the file
    351
    Hello World
    Hello World
    Hello World
    Hello World
    {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf13 0 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \margl1440\margr1440\vieww10800\viewh8400\viewkind 0 BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by asilvester635; 12-13-2015 at 02:32 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Need help reading text from files

    Again that is not a text file. It looks like a RTF file. Be sure when you save the file you specify text.
    asilvester635 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Need help reading text from files

    It worked. Thank you so much!!!

Similar Threads

  1. Reading raw text files
    By DrSaturn in forum Android
    Replies: 0
    Last Post: 02-24-2011, 07:07 PM
  2. Reading text files
    By SuitMyPants in forum Advanced Java
    Replies: 2
    Last Post: 09-27-2010, 05:14 PM
  3. Reading text files
    By SuitMyPants in forum New To Java
    Replies: 2
    Last Post: 09-27-2010, 02:39 PM
  4. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 05:45 AM
  5. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •