Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Reading a line from a file

    Why doesn't this work. This is how the book says to do it, apart from the while statement. Even if i remove the while satement I still get "No line found".

    I am trying to read Lines from a text file which will be stored in an array.

    int Index = 0;

    String DataPath = "D:/Work/java/Mine/Ratings/Data Files";

    String strNames [];
    strHrsNames = new String [240];


    Scanner diskScanner = new Scanner(new File(DataPath + "/Index Files/Test.txt"));


    while (diskScanner.hasNextLine()); {

    strNames[Index]= diskScanner.nextLine();
    Index++;
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading a line from a file

    Actually, you SHOULD be getting an exception as you never initialise strNames.

    And nothing in that code will produce the String "No line found".

    Why don't you post the actual code (using code tags).

  3. #3
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    oops typo
    strHrsNames = new String [240]; should have read

    strNames = new String [240];



    This is the actual code with error message I get and the line numbers

    HorseSort(); // Line 65

    }

    private static void HorseSort() throws ParseException, FileNotFoundException, UnsupportedEncodingException
    {

    String DataPath = "D:/Work/java/Mine/Ratings/Data Files";
    int Index = 0;
    String strHrsNames [];
    strHrsNames = new String [240];
    Scanner diskScanner = new Scanner(new File(DataPath + "/Index Files/Test.txt"));


    while (diskScanner.hasNextLine()); {


    strHrsNames[Index]= diskScanner.nextLine(); //Line 82
    Index++;
    }


    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at horseratings.HorseRatings.HorseSort(HorseRatings.j ava:82)
    at horseratings.HorseRatings.main(HorseRatings.java:6 5)
    Java Result: 1

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading a line from a file

    Create the File reference on a line by itself, rather than in the new Scanner call, and then printout what a call to "exists()", and "length()", and "getAbsolutePath()" on that reference returns.

  5. #5
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    Ok. Did that and this is what I get.


    Exist is true
    Length is 313
    Absolute path is D:\Work\java\Mine\Ratings\Data Files\Index Files\Test.txt

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: Reading a line from a file

    Java Code:
    while (diskScanner.hasNextLine()); {
    That can't be the code you're actually using, that semicolon is not supposed to be there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    Ah, that fixed the No line found message but it still doesn't work. The loop isn't entered.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: Reading a line from a file

    And how do you test that? Post your updated code with the debugging statements you're using to test what is happening, and please use code tags.

    Guide For New Members
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    I put a print statement in the loop and nothing prints.

    while (diskScanner.hasNextLine()) {


    strHrsNames[Index]= diskScanner.nextLine();
    System.out.println("Name is : " + strHrsNames[0]);
    Index++;
    }

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading a line from a file

    Does the file contain at least one newline character? And is it the proper newline character for your operating system?

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: Reading a line from a file

    I did a little test. Input file 'salary.txt' (no newlines):

    Java Code:
    FirstName1 LastName1 assistant 79174.73 FirstName2 LastName2 associate 70817.75 FirstName3 LastName3 associate 69619.0 FirstName4 LastName4 full 116992.43
    Test code:
    Java Code:
    import java.io.*;
    import java.util.*;
     
    public class Test
    {
        public static void main(String[] args) {
        //Throw any exceptions
    		try{
    			Scanner diskScanner = new Scanner(new File("salary.txt"));
    
    			while (diskScanner.hasNextLine()) {
    
    				String line = diskScanner.nextLine();
    				System.out.println(line);
    			}
    			
    		} catch(Throwable t){
    		   t.printStackTrace();
    		}
    	}
    }
    Output is as expected, one line.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    The file was created using note pad under windows 8.1 and contains 23 individual lines not 1 wordwrapped.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading a line from a file

    Quote Originally Posted by gimbal2 View Post
    Output is as expected, one line.
    Okay. I don't use scanner AT ALL (I find it terribly ineffecient and I don't do console user input), and the API docs seem to indicate something else

    "Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present."

    It does not say that if NONE are found it will still return what it HAS buffered already.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading a line from a file

    Quote Originally Posted by peter_62au View Post
    The file was created using note pad under windows 8.1 and contains 23 individual lines not 1 wordwrapped.
    You don't STILL have the file open in Notepad while running the tests, do you?

    I don't know if Notepad creates a lock or not, but most other Windows programs do.

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: Reading a line from a file

    I don't use it either, that's why when I start to grow huge question marks above my head I just slap it into my sandbox test environment ;)

    (that's an expensive way to describe that I have one directory named 'sandbox' filled with source files and test data files in which I can just quickly compile and run stuff on the command line that people post in forums to see what is going on).

    Test #2: keeping the file open in notepad, edited or unedited. Java program works as expected ;)
    Last edited by gimbal2; 12-13-2013 at 11:27 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    The file is not open in notepad. If I have the while statement in it just skips the loop and runs the rest of the module, if I remove the loop and just try to read 1 line using:

    strHrsNames[Index]= diskScanner.nextLine();

    then it just stops there and doesn't run any more code past that line.
    But that's how it says to do it in the book I'm reading.

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: Reading a line from a file

    Whatever you're doing, it seems to keep proving that you are feeding the application an empty file and not that test file with content :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Reading a line from a file

    Can you show the full HorseSort method as it stands now, complete with debugging statements showing the size of the file etc.
    And please (please!) use code tags (as already requested by gimbal).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    Removed unformatted code. Reposted properly formatted.
    Last edited by peter_62au; 12-13-2013 at 10:08 PM. Reason: Unformatted code

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Reading a line from a file

    Code tags?
    Formatted code is pretty much a requirement to see exactly what's going on (and you can remove the line numbers).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. reading particular line from textfile file
    By karrydhawan in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 12:22 PM
  2. Reading Next Line of File
    By smitsky in forum New To Java
    Replies: 5
    Last Post: 12-04-2011, 05:35 PM
  3. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  4. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 02:57 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

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