Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old October 8th, 2008, 04:03 AM
Newbie
 
Join Date: Sep 2008
Posts: 7
Default 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


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
Reply
  #2  
Old October 8th, 2008, 08:45 AM
Administrator
 
Join Date: Sep 2006
Posts: 11,443
Default

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

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.
Reply
  #4  
Old October 8th, 2008, 10:33 AM
JosAH's Avatar
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,760
Default

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
Reply
  #5  
Old October 8th, 2008, 10:39 AM
JosAH's Avatar
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,760
Default

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

kind regards,

Jos (moderator)
Reply
  #6  
Old October 8th, 2008, 02:30 PM
myusernotyours's Avatar
Member
 
Join Date: Nov 2007
Posts: 114
Default

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...
Reply
  #7  
Old October 9th, 2008, 12:38 AM
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Age: 21
Posts: 2,166
Default

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.