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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaifaCarina
    New Member
    • Sep 2007
    • 13

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

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

    Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    
    public class Trial {
    	
        public static void main(String [] args) throws IOException {
    
          	BufferedReader s = null;
    		FileOutputStream out;
            PrintStream p;
    
    		String CDtitle[] = new String[1000],
    				category[] = new String [1000],
    				container[] = new String [1000],
    				location[] = new String [1000];
    
            int h, k, i=0,count=0, temporary;
            String tmp, inputCDtitle, inputcategory, inputcontainer, inputlocation;
    
              	CsvTest test = new CsvTest();
        		test.readFile(CDtitle,category,container,location);      
            
            
            	inputCDtitle = JOptionPane.showInputDialog("Enter item");
            	inputcategory = JOptionPane.showInputDialog("Enter Category");
            	inputcontainer = JOptionPane.showInputDialog("Enter container");
            	inputlocation = JOptionPane.showInputDialog("Enter location");
            	for(i=0;i<CDtitle.length;i++){
    	        	if (CDtitle[i]!=null){
    	        	
    	        	 System.out.println(CDtitle[i] + " = " + category[i]+ " = " + container[i]+ " = " + location[i]);
    	        	 count++;
           			}
           		}
           		
            	System.out.println(count);
            	CDtitle[count+1] = inputCDtitle;
            	category[count+1] = inputcategory;
            	container[count+1] = inputcontainer;
            	location[count+1] = inputlocation;
            	try {
            	out = new FileOutputStream("fileInput.txt");
            	p = new PrintStream(out);
       		
       			for(h=0; h<CDtitle.length;h++){
       				if (CDtitle[h]!=null){
    	    			p.println(CDtitle[h]+ "," + category[h] + "," + container[h]+ "," + location[h]);
    	    		}
    			}
            	p.close();
            	
                
            } finally {
            	 try {
            if (s != null)
              s.close();
    	      }
    	      catch (IOException ex) {
    	        ex.printStackTrace();
    	      }
                
            } 
        	test.readFile(CDtitle,category,container,location);  
       	 	
    
            for(i=0;i<CDtitle.length;i++){
            	if (CDtitle[i]!=null){
            	    System.out.println(CDtitle[i] + " = " + category[i]+ " = " + container[i]+ " = " + location[i]);
          	  }
            }
       		
        }
        
    }

    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.*;
    
    public class CsvTest {
    
      public void readFile(String CDtitle[],String category[], String container[], String location[]) {
    
        BufferedReader br = null;
    
        try {
          
          br = new BufferedReader(new FileReader("fileInput.txt"));
          String line = null;
          int i=0;
          while ((line = br.readLine()) != null) {
    
            StringTokenizer st = new StringTokenizer(line, ",");              
             
                CDtitle[i] = st.nextToken(); 
                category[i] = st.nextToken(); 
                container[i] = st.nextToken();   
                location[i] = st.nextToken();           
         		i++;
             
          }
        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
        finally {
          try {
            if (br != null)
              br.close();
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
        }
    
    
      }
    	
    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    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...

    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    Is already been linked, since you have

    Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    You can erase it...

    I am using jdk 1.6.03

    And there is no problem... ;-)


    Sukatoa, Shadow Shaman

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      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.

      Comment

      • HaifaCarina
        New Member
        • Sep 2007
        • 13

        #4
        Originally posted by sukatoa

        I've test it in a single file,
        how do i test it in a single file?

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          Originally posted by HaifaCarina
          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...

          Comment

          Working...