Java:noSuchElementException.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chetah
    New Member
    • Sep 2008
    • 10

    Java:noSuchElementException.

    text file
    Dietel10004 How to Program In Java R IN Dietal & Dietal Prentice Hall 06-09-08
    Flanag0204 Java In a Nutshell C OUT David Flanagan O'Reilly 03-06-20
    END
    99100452 John Smith #5 Hillview Drive, Tunapuna UnderGrad 5
    02154632 Chris Chami #16 Insame Lane, St. Anns Lecturer 0
    END

    Data are separated by tabs


    [code=java]
    public class Book{
    private String ISBN;
    private String title;
    private String type;
    private String status;
    private String author;
    private String publisher;
    private String dateborrowed;

    public Book( String isb, String tit, String typ, String sta, String aut, String pub, String dateb )
    {
    ISBN = isb;
    title = tit;
    type = typ;
    status = sta;
    author = aut;
    publisher = pub;
    dateborrowed = dateb;
    }
    public String toString(){
    String str;
    str = "ISBN" + ISBN+"\n"+
    "title " + title+"\n"+
    "type " + type+"\n"+
    "status " + status+"\n"+
    "author " + author+"\n"+
    "publisher " + publisher+"\n"+
    "dateborrow ed " + dateborrowed+"\ n";
    return str;

    }
    }//End Book class [/code]
    [code=java]
    import java.io.*;
    import java.util.*;
    public class Test1{
    static Book[] book = new Book [ 100 ];
    public static void main(String [] args)throws IOException{

    Scanner input = new Scanner(new FileReader("db. txt"));


    input.useDelimi ter("\t"); //Set scanner to break on tabs

    int a = 0;
    String x = input.next();

    while(!(x.equal s("END"))){
    book[a] = new Book(x, input.next(),in put.next(),inpu t.next(),input. next(),input.ne xt(),input.next ()); //noSuchElementEx ception
    a++;
    x = input.next();
    }



    for(int i = 0; i < a; i++){
    System.out.prin tln(book[i].toString());
    }

    }//end class main
    }//end class test
    [/code]
    I am trying to read the text file above and store the first 2 rows in book and array of objects. Every time I run the program it gives a noSuchElementEx ception. I am reading 7 text strings , I cannot understan why. Please help.
    Last edited by Nepomuk; Oct 8 '08, 11:31 PM. Reason: Added [CODE] tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    That exception is thrown when no more tokens are available for the scanner.

    Comment

    • myusernotyours
      New Member
      • Nov 2007
      • 188

      #3
      Hi,

      The scanner fails to find any more tabs before reading END. That's why it fails. It means all your data is not delimited by tabs.
      Check the documentation for the Scanner class.

      Regards,

      Alex.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Why not write a little debug message that prints out what the Scanner has just read?
        Something like this will do:

        Code:
        private String next(Scanner scan) {
           String result= scan.next();
           System.out.println("read: '"+result+"'");
           return result;
        }
        Use this method in your main() instead of input.next() directly and see what happens.

        kind regards,

        Jos

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          FYI: chetah also posted this question on the Sun Java forums.

          kind regards,

          Jos (moderator)

          Comment

          • myusernotyours
            New Member
            • Nov 2007
            • 188

            #6
            Originally posted by JosAH
            FYI: chetah also posted this question on the Sun Java forums.

            kind regards,

            Jos (moderator)
            Intrestingly there is this too...

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by myusernotyours
              Intrestingly there is this too...
              ... and in both that thread and in this one here, the [CODE...[/CODE] tags had to be added for you. Now, I'm giving you an official warning: Use the CODE tags! That means, put the begin tag [CODE] before and the end tag [/CODE] after your code. Failing to do so makes your code much harder to read and therefore we insist, that they are used here.

              Greetings,
              Nepomuk (Moderator)

              Comment

              Working...