Results 1 to 8 of 8
  1. #1
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default 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;
      }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    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)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you put a catch block on processLineByLine() method and see any exceptions thrown there. You only handle the FileNotFoundException.

  5. #5
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    I got it to work:), thanks for the help

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    808
    Rep Power
    6

    Default

    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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  3. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  4. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 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
  •