Results 1 to 8 of 8
Thread: can't link to text file
- 04-20-2009, 04:44 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
can't link to text file
Hi.
I can't figure out what's wrong with this program.
I have a text file in the same folder as the program, but the program gives my an error message everytime I run it that says:
java.util.NoSuchElementExeption
thankful for help
----------------------------------------------------------------
Java Code:import java.io.*; import java.util.Scanner; public final class NameNumber { public static void main(String... aArgs) throws FileNotFoundException { ReadWithScanner parser = new ReadWithScanner("mytext.txt"); parser.processLineByLine(); log("Done."); } public NameNumber(String mytext){ fFile = new File(mytext); } public final void processLineByLine() throws FileNotFoundException { Scanner scanner = new Scanner(fFile); try { while ( scanner.hasNextLine() ){ processLine( scanner.nextLine() ); } } finally { scanner.close(); } } protected void processLine(String aLine){ Scanner scanner = new Scanner(aLine); scanner.useDelimiter("="); if ( scanner.hasNext() ){ String name = scanner.next(); String value = scanner.next(); log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) ); } else { log("Empty or invalid line. Unable to process."); } scanner.close(); } private final File fFile; private static void log(Object aObject){ System.out.println(String.valueOf(aObject)); } private String quote(String aText){ String QUOTE = "'"; return QUOTE + aText + QUOTE; } }
- 04-20-2009, 04:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you copy-paste the complete error message here to see?
- 04-20-2009, 05:02 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Here's the full error message:
Java Code:D:\Java Temp>java NameNumber Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at ReadWithScanner.processLine(ReadWithScanner.java:37) at ReadWithScanner.processLineByLine(ReadWithScanner.java:22) at NameNumber.main(NameNumber.java:8)
- 04-20-2009, 05:09 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you put a catch block on processLineByLine() method and see any exceptions thrown there. You only handle the FileNotFoundException.
- 04-20-2009, 07:32 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
I got it to work:), thanks for the help
- 04-20-2009, 07:41 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What's the issue you have there, and how did you solve it?
- 04-20-2009, 07:51 AM #7
Hi,
I copeid and pasted the code and placed the text file in the same location.I dont find any exceptions.
My text filecontent is
eno=ramya
output is
Name is : 'eno', and Value is : 'ramya'
Done.
-Regards
Ramya
- 04-20-2009, 08:22 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
May be our thread starters' file content is different, with delimiters he used in.
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks