469,097 Members | 1,600 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

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

Post your reply

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

Similar topics

reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.