Results 1 to 3 of 3
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default File input throwing NoSuchElementException exception

    I am trying to read a file and store its contents into my LinkedList.
    The file contains each object of the linked list on a new line, and each element in the object is seperated by a comma and no whitespace. Ive set the delimeter up to just have "," as there are titles seperated by whitespace and i dont want it to read these as two seperate tokens.

    Java Code:
    public Registry()
        {
            assignmentList = new LinkedList<Assignment>();
            try
            {
                File inFile = new File("Assignment.txt");
                Scanner in = new Scanner(inFile);
                in.useDelimiter(",");
                while(in.hasNextLine())
                {
                    String mTitle = in.next();
                    String mIdentifier = in.next();
                    String aTitle = in.next();
                    String dateSet = in.next();
                    String dueDate = in.next();
                    String aAuthor = in.next();
                    String weighting = in.next();
                    addAssignment(new Assignment(mTitle,mIdentifier,aTitle,dateSet,dueDate,aAuthor,weighting));
                }
                in.close();
            }
            catch(FileNotFoundException exception)
            {
                System.out.println("FileNotFoundException " + exception);
            }
            catch(IOException exception)
            {
                System.out.println("IOException " + exception);
            }
            
            
        }
    An example of the .txt file is
    Java 2,ASM 1001,Programming Project,13/05/2012,20/05/2012,Joe Bloggs,20%
    Java 1,ASM 1002,Another Programming Project,27/05/2012,05/06/2012,Joe Bloggs,20%

    and the error message is

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at Registry.<init>(Registry.java:34)
    at FileTester.main(FileTester.java:5)
    Java Result: 1

    at Registry.<init>(Registry.java:34) refers to this line String dateSet = in.next(); if that helps

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: File input throwing NoSuchElementException exception

    Isn't it be better to read the text line by line and then split each line into the individual data? Using the program above if you check the weighting variable value it will contains value like 20% Java 1 because you only tell the scanner to delimit the entry by comma.

    And at some point this will produce the NoSuchElementException because no more data to read when you call the next() method. It's because the weighting took two values instead of one.

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: File input throwing NoSuchElementException exception

    I have changed the format of the .txt file now to add a delimter at the start of each line which now works.
    I thought as i was saying while(in.hasNextLine) it would read all the tokens from that line and then start again from the new line without the need for a delimeter at the start. I guess not, also i will have a look what you suggested about reading line by line and splitting it.

Similar Threads

  1. Program not throwing exception
    By aedara in forum New To Java
    Replies: 3
    Last Post: 06-28-2011, 06:55 AM
  2. Replies: 6
    Last Post: 03-11-2011, 04:53 AM
  3. Exception throwing
    By andy16 in forum New To Java
    Replies: 9
    Last Post: 06-07-2010, 06:57 PM
  4. Replies: 5
    Last Post: 02-27-2009, 06:19 AM
  5. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 10:35 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
  •