 | 
October 8th, 2008, 04:03 AM
| | Newbie | | Join Date: Sep 2008
Posts: 7
| | 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 -
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"+
-
"dateborrowed " + dateborrowed+"\n";
-
return str;
-
-
}
-
}//End Book class
-
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.useDelimiter("\t"); //Set scanner to break on tabs
-
-
int a = 0;
-
String x = input.next();
-
-
while(!(x.equals("END"))){
-
book[a] = new Book(x, input.next(),input.next(),input.next(),input.next(),input.next(),input.next()); //noSuchElementException
-
a++;
-
x = input.next();
-
}
-
-
-
-
for(int i = 0; i < a; i++){
-
System.out.println(book[i].toString());
-
}
-
-
}//end class main
-
}//end class test
-
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
| 
October 8th, 2008, 08:45 AM
| | Administrator | | Join Date: Sep 2006
Posts: 11,312
| |
That exception is thrown when no more tokens are available for the scanner.
| 
October 8th, 2008, 08:49 AM
|  | Member | | Join Date: Nov 2007
Posts: 114
| |
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.
| 
October 8th, 2008, 10:33 AM
| | Moderator | | Join Date: Mar 2007 Location: Voorschoten, the Netherlands Age: 52
Posts: 8,480
| |
Why not write a little debug message that prints out what the Scanner has just read?
Something like this will do: -
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
| 
October 8th, 2008, 10:39 AM
| | Moderator | | Join Date: Mar 2007 Location: Voorschoten, the Netherlands Age: 52
Posts: 8,480
| |
FYI: chetah also posted this question on the Sun Java forums.
kind regards,
Jos (moderator)
| 
October 8th, 2008, 02:30 PM
|  | Member | | Join Date: Nov 2007
Posts: 114
| | 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...
| 
October 9th, 2008, 12:38 AM
|  | Moderator | | Join Date: Aug 2007 Location: Germany Age: 21
Posts: 1,973
| | 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)
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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 network members.
|