Results 1 to 9 of 9
- 12-23-2012, 06:53 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 35
- Rep Power
- 0
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:
This I then put in a JLabel to display it in a JFrame.Java Code:mReadmeReader = new Scanner(new File("README.txt")); while(mReadmeReader.hasNextLine()) { String mHelpText += mReadmeReader.nextLine() + "<br />"; } return "<html><body>" + mHelpText + "</body></html>";
But when I edit the txt file the scanner stops reading the lines in teh file and this code only returns this:
Would anybody know why this happens to me?<html><body></body></html>
-
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?
- 12-23-2012, 07:23 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 35
- Rep Power
- 0
Re: Scanner problem when reading file
my try catch looks like this:
and I get no exceptions but I do println the return value and its what is 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); }
What I do is open the textfile in notepad and change the text and just save it<html><body></body></html>
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.
-
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).
- 12-23-2012, 07:37 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 35
- Rep Power
- 0
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.
- 12-23-2012, 07:54 PM #6
Member
- Join Date
- Dec 2012
- Posts
- 35
- Rep Power
- 0
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 !
-
Re: Scanner problem when reading file
Perhaps you want to use the Scanner constructor that lets you also pass in a charsetName.
- 12-23-2012, 08:32 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 35
- Rep Power
- 0
Re: Scanner problem when reading file
I do use this now:
but thenJava Code:mReadmeReader = new Scanner(new File("README.txt"), "CP850");
å = Õ
ä = õ
ö = ÷
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.
- 12-24-2012, 01:56 PM #9
Member
- Join Date
- Dec 2012
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Scanner malfunction - Reading a file
By Noceo in forum New To JavaReplies: 10Last Post: 10-30-2012, 06:49 PM -
Reading a file with Scanner
By Games2Design in forum New To JavaReplies: 5Last Post: 10-04-2012, 10:10 AM -
File reading with Scanner
By mik in forum New To JavaReplies: 2Last Post: 08-31-2012, 09:44 AM -
Scanner not reading every character in a file?
By Brandonhspace in forum New To JavaReplies: 5Last Post: 06-07-2012, 07:57 AM -
Reading file problem using Scanner
By nfsmwbe in forum New To JavaReplies: 18Last Post: 01-04-2012, 03:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks