473,395 Members | 1,386 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,395 software developers and data experts.

Help! Exception in thread "main" java.util.NoSuchElementException

here's the complete lines of errors..
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.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...

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class Trial {
  7.  
  8.     public static void main(String [] args) throws IOException {
  9.  
  10.           BufferedReader s = null;
  11.         FileOutputStream out;
  12.         PrintStream p;
  13.  
  14.         String CDtitle[] = new String[1000],
  15.                 category[] = new String [1000],
  16.                 container[] = new String [1000],
  17.                 location[] = new String [1000];
  18.  
  19.         int h, k, i=0,count=0, temporary;
  20.         String tmp, inputCDtitle, inputcategory, inputcontainer, inputlocation;
  21.  
  22.               CsvTest test = new CsvTest();
  23.             test.readFile(CDtitle,category,container,location);      
  24.  
  25.  
  26.             inputCDtitle = JOptionPane.showInputDialog("Enter item");
  27.             inputcategory = JOptionPane.showInputDialog("Enter Category");
  28.             inputcontainer = JOptionPane.showInputDialog("Enter container");
  29.             inputlocation = JOptionPane.showInputDialog("Enter location");
  30.             for(i=0;i<CDtitle.length;i++){
  31.                 if (CDtitle[i]!=null){
  32.  
  33.                  System.out.println(CDtitle[i] + " = " + category[i]+ " = " + container[i]+ " = " + location[i]);
  34.                  count++;
  35.                    }
  36.                }
  37.  
  38.             System.out.println(count);
  39.             CDtitle[count+1] = inputCDtitle;
  40.             category[count+1] = inputcategory;
  41.             container[count+1] = inputcontainer;
  42.             location[count+1] = inputlocation;
  43.             try {
  44.             out = new FileOutputStream("fileInput.txt");
  45.             p = new PrintStream(out);
  46.  
  47.                for(h=0; h<CDtitle.length;h++){
  48.                    if (CDtitle[h]!=null){
  49.                     p.println(CDtitle[h]+ "," + category[h] + "," + container[h]+ "," + location[h]);
  50.                 }
  51.             }
  52.             p.close();
  53.  
  54.  
  55.         } finally {
  56.              try {
  57.         if (s != null)
  58.           s.close();
  59.           }
  60.           catch (IOException ex) {
  61.             ex.printStackTrace();
  62.           }
  63.  
  64.         } 
  65.         test.readFile(CDtitle,category,container,location);  
  66.  
  67.  
  68.         for(i=0;i<CDtitle.length;i++){
  69.             if (CDtitle[i]!=null){
  70.                 System.out.println(CDtitle[i] + " = " + category[i]+ " = " + container[i]+ " = " + location[i]);
  71.             }
  72.         }
  73.  
  74.     }
  75.  
  76. }

Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.*;
  6.  
  7. public class CsvTest {
  8.  
  9.   public void readFile(String CDtitle[],String category[], String container[], String location[]) {
  10.  
  11.     BufferedReader br = null;
  12.  
  13.     try {
  14.  
  15.       br = new BufferedReader(new FileReader("fileInput.txt"));
  16.       String line = null;
  17.       int i=0;
  18.       while ((line = br.readLine()) != null) {
  19.  
  20.         StringTokenizer st = new StringTokenizer(line, ",");              
  21.  
  22.             CDtitle[i] = st.nextToken(); 
  23.             category[i] = st.nextToken(); 
  24.             container[i] = st.nextToken();   
  25.             location[i] = st.nextToken();           
  26.              i++;
  27.  
  28.       }
  29.     }
  30.     catch (FileNotFoundException ex) {
  31.       ex.printStackTrace();
  32.     }
  33.     catch (IOException ex) {
  34.       ex.printStackTrace();
  35.     }
  36.     finally {
  37.       try {
  38.         if (br != null)
  39.           br.close();
  40.       }
  41.       catch (IOException ex) {
  42.         ex.printStackTrace();
  43.       }
  44.     }
  45.  
  46.  
  47.   }
  48.  
  49. }
Feb 26 '08 #1
4 16687
sukatoa
539 512MB
Ive test your code,
There's nothing wrong with your code except that the file need for your program doesn't exist... At first execution...

I've test it in a single file,
Asking for Item, category, container and location in JOptionPane mode...

Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5.  
Is already been linked, since you have

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.*;
You can erase it...

I am using jdk 1.6.03

And there is no problem... ;-)


Sukatoa, Shadow Shaman
Feb 26 '08 #2
r035198x
13,262 8TB
1.) StringTokenizer is old. Use the String.split method instead.
2.) Arrays are not the best for object oriented modelling. Create a CD class with title, category e.t.c as fields and store them in an ArrayList<CD>.
3.) Do not use a FileOutputStream for writing characters. Use BufferedWriter or PrintWriter instead.
Feb 26 '08 #3

I've test it in a single file,
how do i test it in a single file?
Feb 29 '08 #4
sukatoa
539 512MB
how do i test it in a single file?
The codes above, i mean the code that you have been posted at the top...
Save them in a file, with filename exactly the same with the public class...

Don't forget to follow what r035198x said...

Update us,
Sukatoa...
Feb 29 '08 #5

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

Similar topics

0
by: Geetu | last post by:
I am trying to jar up a file and getting this exception, not sure what could be wrong. The same piece of code works fine in few machines but there is one machine where this piece of code gives this...
0
by: daniel li | last post by:
I got another convert issue. This is the same project as I posted yesterday, and received a perfect response. The conversion threw a SupportClass for me and one of the data type is...
4
by: puntino | last post by:
Hi I have created my Alarm, at compile time I don't have any problem, but at run time I receive the follo wing messages: Exception in thread "main" java.lang.NullPointerException at...
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...
6
by: jri | last post by:
Hello, I got an assignement for school where I have to make a game with matches. This is what I got: import javax.swing.JOptionPane; import java.util.Scanner; public class Lucifer
6
oll3i
by: oll3i | last post by:
when i run my jsp file i get an error SEVERE: Servlet.service() for servlet jsp threw exception java.util.MissingResourceException: Can't find bundle for base name MessagesBundle, locale pl_PL...
4
by: sukatoa | last post by:
This was my first time to encouter this kind of exception.... that exception appears when i invoked the the method below. private final String encrypting(String enc){ int...
1
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
6
by: monkey0525 | last post by:
Each time I run the ProgTwo program, it displays: 1. Display one product 2. Display all products 3. Add a new CD 6. Exit Enter your choice: Exception in thread "main"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.