Connecting Tech Pros Worldwide Help | Site Map

Java:noSuchElementException.

  #1  
Old October 8th, 2008, 04:03 AM
Newbie
 
Join Date: Sep 2008
Posts: 7
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


Expand|Select|Wrap|Line Numbers
  1. public class Book{
  2.  private String ISBN;
  3.  private String title;
  4.  private String   type;
  5.  private String status;
  6.  private String author;
  7.  private String publisher;
  8.  private String dateborrowed;
  9.  
  10.  public Book( String isb, String tit, String typ, String sta, String aut, String pub, String dateb )
  11.  {
  12.   ISBN         = isb;
  13.   title        = tit;
  14.   type         = typ;
  15.   status       = sta;
  16.   author       = aut;
  17.   publisher    = pub;
  18.   dateborrowed = dateb;
  19.  }
  20. public String toString(){
  21.   String str;
  22.   str = "ISBN" + ISBN+"\n"+
  23.   "title "       + title+"\n"+
  24.   "type   "      + type+"\n"+
  25.   "status  "     + status+"\n"+
  26.   "author   "    + author+"\n"+
  27.   "publisher "   + publisher+"\n"+
  28.   "dateborrowed " + dateborrowed+"\n";
  29.   return str;
  30.  
  31.  }
  32. }//End Book class 
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class Test1{
  4.  static Book[] book = new Book [ 100 ];
  5.  public static void main(String [] args)throws IOException{
  6.  
  7.  Scanner input = new Scanner(new FileReader("db.txt"));
  8.  
  9.  
  10.   input.useDelimiter("\t");  //Set scanner to break on tabs
  11.  
  12.   int a = 0;
  13.   String x = input.next();
  14.  
  15.   while(!(x.equals("END"))){
  16.    book[a] = new Book(x, input.next(),input.next(),input.next(),input.next(),input.next(),input.next()); //noSuchElementException
  17.    a++;
  18.    x = input.next();
  19.   }
  20.  
  21.  
  22.  
  23.   for(int i = 0; i < a; i++){
  24.    System.out.println(book[i].toString());
  25.   }
  26.  
  27.   }//end class main 
  28. }//end class test
  29.  
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 noSuchElementException. I am reading 7 text strings , I cannot understan why. Please help.

Last edited by Nepomuk; October 9th, 2008 at 12:31 AM. Reason: Added [CODE] tags
  #2  
Old October 8th, 2008, 08:45 AM
Lives Here
 
Join Date: Sep 2006
Posts: 12,085

re: Java:noSuchElementException.


That exception is thrown when no more tokens are available for the scanner.
  #3  
Old October 8th, 2008, 08:49 AM
myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168

re: Java:noSuchElementException.


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.
  #4  
Old October 8th, 2008, 10:33 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Java:noSuchElementException.


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

Expand|Select|Wrap|Line Numbers
  1. private String next(Scanner scan) {
  2.    String result= scan.next();
  3.    System.out.println("read: '"+result+"'");
  4.    return result;
  5. }
  6.  
Use this method in your main() instead of input.next() directly and see what happens.

kind regards,

Jos
  #5  
Old October 8th, 2008, 10:39 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Java:noSuchElementException.


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

kind regards,

Jos (moderator)
  #6  
Old October 8th, 2008, 02:30 PM
myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168

re: Java:noSuchElementException.


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

kind regards,

Jos (moderator)
Intrestingly there is this too...
  #7  
Old October 9th, 2008, 12:38 AM
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,468

re: Java:noSuchElementException.


Quote:
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)
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
NoSuchElementException Error sentinel Loop jimgym1989 answers 4 September 18th, 2008 04:16 PM
Please help!! I'm stuck with java code...API not helping. BlueroY answers 3 September 17th, 2008 08:12 PM
Help! Exception in thread "main" java.util.NoSuchElementException HaifaCarina answers 4 February 29th, 2008 12:51 PM
How to avoid EOFException while using readFully() in java VeeraLakshmi answers 0 March 26th, 2007 04:07 PM