469,332 Members | 6,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Java:noSuchElementException.

10
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.
Oct 8 '08 #1
6 5606
r035198x
13,262 8TB
That exception is thrown when no more tokens are available for the scanner.
Oct 8 '08 #2
myusernotyours
188 100+
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.
Oct 8 '08 #3
JosAH
11,448 Expert 8TB
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
Oct 8 '08 #4
JosAH
11,448 Expert 8TB
FYI: chetah also posted this question on the Sun Java forums.

kind regards,

Jos (moderator)
Oct 8 '08 #5
myusernotyours
188 100+
FYI: chetah also posted this question on the Sun Java forums.

kind regards,

Jos (moderator)
Intrestingly there is this too...
Oct 8 '08 #6
Nepomuk
3,112 Expert 2GB
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)
Oct 8 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Luc Mazardo | last post: by
reply views Thread by nadeemabdulhamid | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.