Results 1 to 9 of 9
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Scanner problem when reading file

    Hi, I am using Scanner to read the contents of a txt file and it is working great with this code:

    Java Code:
    mReadmeReader = new Scanner(new File("README.txt"));
    
    while(mReadmeReader.hasNextLine())
    {
          String mHelpText +=  mReadmeReader.nextLine() + "<br />";
    }
            
    return "<html><body>" + mHelpText + "</body></html>";
    This I then put in a JLabel to display it in a JFrame.

    But when I edit the txt file the scanner stops reading the lines in teh file and this code only returns this:

    <html><body></body></html>
    Would anybody know why this happens to me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Scanner problem when reading file

    Are you seeing any exceptions? You don't have any empty catch blocks do you? How do you change the text file? Do you change the text file name? Have you either used a debugger or added println statements in your code to see what may not be working correctly?

  3. #3
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Scanner problem when reading file

    my try catch looks like this:

    Java Code:
    try
            {
                mReadmeReader = new Scanner(new File("README.txt"));
            }
            catch (FileNotFoundException e)
            {
                System.err.println("Error: could not find file path!\nMessage: " + e + "\nPlease make sure that the path to this folder from the projects main folder exists.");
            }
            catch (IOException e)
            {
                System.err.println("Error: could not create/write to file. Check your access to file or folder. Message: " + e);
            }
    and I get no exceptions but I do println the return value and its what is this:
    <html><body></body></html>
    What I do is open the textfile in notepad and change the text and just save it

    it does not get in to this while loop:

    Java Code:
    while(mReadmeReader.hasNextLine())
            {
                mHelpText +=  mReadmeReader.nextLine() + "<br />";
            }
    Last edited by Gatsu; 12-23-2012 at 07:26 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Scanner problem when reading file

    Put println statements inside of the method, specifically get the String returned from the nextLine() and print it out, and then add that same String to your concatenating String (better to use a StringBuilder for this).

  5. #5
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Scanner problem when reading file

    I can not println the nextLine() because it tells me this:
    Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No line found

    Scanner suddenly thinks there are no lines in the file, but its so strange for me.. it works with the .txt that I copied in from another folder.

    I can edit the text and add like 4 lines of code but if I paste what I need to be in the file it don't work.....
    Last edited by Gatsu; 12-23-2012 at 07:42 PM.

  6. #6
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Scanner problem when reading file

    oh my god, this has to do with that I am using letters, I think Scanner has a problem with reading these !

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Scanner problem when reading file

    Perhaps you want to use the Scanner constructor that lets you also pass in a charsetName.

  8. #8
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Scanner problem when reading file

    I do use this now:
    Java Code:
    mReadmeReader = new Scanner(new File("README.txt"), "CP850");
    but then
    =
    =
    =

    I read many on places that said CP850 is the correct one to use. Would you happen to know why it gives me this?
    Last edited by Gatsu; 12-24-2012 at 12:57 PM.

  9. #9
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Scanner problem when reading file

    can it be that JLabel can not hold , because this is what I display the text with?

Similar Threads

  1. Scanner malfunction - Reading a file
    By Noceo in forum New To Java
    Replies: 10
    Last Post: 10-30-2012, 06:49 PM
  2. Reading a file with Scanner
    By Games2Design in forum New To Java
    Replies: 5
    Last Post: 10-04-2012, 10:10 AM
  3. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 09:44 AM
  4. Scanner not reading every character in a file?
    By Brandonhspace in forum New To Java
    Replies: 5
    Last Post: 06-07-2012, 07:57 AM
  5. Reading file problem using Scanner
    By nfsmwbe in forum New To Java
    Replies: 18
    Last Post: 01-04-2012, 03:26 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
  •