473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 5736
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

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

Similar topics

14
by: Luc Mazardo | last post by:
The following code works with g++ 2.75 but i've some troubles with compiling with g++ 3.0. Thanks in advance if you have a solution. Here is the error message. main.hpp:28: specializing...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
0
by: nadeemabdulhamid | last post by:
Hello, I'm trying to write some Java code that will launch a python interpreter shell and pipe input/output back and forth from the interpreter's i/o streams and the Java program. The code posted...
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
4
by: HaifaCarina | last post by:
here's the complete lines of errors.. Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:332) at...
3
by: BlueroY | last post by:
hi, I'm working on an exercise, i did a lot of work already and i just can't figure where I'm going wrong, this is what I'm trying to achieve Sample IO...
4
by: jimgym1989 | last post by:
import java.io.*; import java.util.*; public class tryCountLetter { public static void main(Stringargs) throws FileNotFoundException{ Scanner inFile = new Scanner(new...
1
by: viswanathtvs | last post by:
java.util.NoSuchElementException javax.faces.el.EvaluationException: java.util.NoSuchElementException at...
2
by: cioccolatina | last post by:
Hey guys, is there anyone who could help me..? I have file ExpressionBinaryTree.java : /** class ExpressionBinaryTree * uses a binary tree to represent binary expressions * does not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.