473,799 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help! Exception in thread "main" java.util.NoSuc hElementExcepti on

13 New Member
here's the complete lines of errors..
Exception in thread "main" java.util.NoSuc hElementExcepti on
at java.util.Strin gTokenizer.next Token(StringTok enizer.java:332 )
at CsvTest.readFil e(CsvTest.java: 26)
at Trial.main(Tria l.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 16724
sukatoa
539 Contributor
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 MVP
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 FileOutputStrea m for writing characters. Use BufferedWriter or PrintWriter instead.
Feb 26 '08 #3
HaifaCarina
13 New Member

I've test it in a single file,
how do i test it in a single file?
Feb 29 '08 #4
sukatoa
539 Contributor
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
7091
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 exception , any help is very much appreciated, thanks ......java.util.zip.ZipExce ption: duplicate entry: META-INF/ java.util.zip.ZipException: duplicate entry: META-INF/ at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:160)...
0
3679
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 CollectionSupport, this is to my understanding to replace Java.Util.Collection. In my code: public EcomWeb.rx_transactions.order.GroverOrder getGroverOrders(EcomWeb.domain.WebOrder webOrder) { System.Console.Out.WriteLine("OrderFactory. getGroverOrders( )...
4
5449
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 java.util.Timer.sched(Unknown Source) at java.util.Timer.scheduleAtFixedRate(Unknown Source) at Alarm.AlarmExecute(Alarm.java:20) at Test.main(Test.java:11) (With <- I have pointed out the line 11 of test.java and line 20 of alarm.java)
3
2407
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 InputMismatch private void throwFor() { skipped = false; if ((sourceClosed) &&...
6
13843
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
6960
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 at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1508) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1262) at java.util.ResourceBundle.getBundle(ResourceBundle.java:789) at...
4
7237
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 declen=array1.length; String temp=enc; for(int x=-1;x++<declen;){ temp=temp.replaceAll(String.valueOf(array2),String .valueOf(array1));
1
13742
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
3070
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" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1461)
0
9685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10473
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.