473,544 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java:noSuchElem entException.

10 New Member
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 noSuchElementEx ception. I am reading 7 text strings , I cannot understan why. Please help.
Oct 8 '08 #1
6 5742
r035198x
13,262 MVP
That exception is thrown when no more tokens are available for the scanner.
Oct 8 '08 #2
myusernotyours
188 New Member
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 Recognized Expert MVP
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 Recognized Expert MVP
FYI: chetah also posted this question on the Sun Java forums.

kind regards,

Jos (moderator)
Oct 8 '08 #5
myusernotyours
188 New Member
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 Recognized Expert Specialist
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
1740
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 `struct std::hash<std::string>' in different namespace /usr/include/g++-v3/ext/stl_hash_fun.h:68: from definition of `template<class
0
3124
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 url,first it will ask for authentication.After giving username and password,the authentication will be confirmed and if the site has access right as...
0
1626
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 below seems to work just fine under Mac OS X; however, under Windows XP (Java 1.6 and Python 2.5 installed) the program hangs when I give a command...
3
2382
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 class: (one I added all the asteriss to. // If we are at the end of input then NoSuchElement; // If there is still input left then...
4
16697
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 CsvTest.readFile(CsvTest.java:26) at Trial.main(Trial.java:24) I have no idea what to change here because I don't know what is wrong..please help me out... ...
3
1743
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 ******************************************************************************* Welcome to PeopleSoft 2 MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it a Enter the student number...
4
2556
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 FileReader("D:\\My Documents\\Java Saves\\try.txt")); int counter = 0;
1
3896
by: viswanathtvs | last post by:
java.util.NoSuchElementException javax.faces.el.EvaluationException: java.util.NoSuchElementException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at...
2
7362
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 implement BinaryTree - all iterators return String */ package foundations;
0
7438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7374
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7720
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5309
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3422
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1850
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.