GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Nov-2008, 17:55
gadisandeep gadisandeep is offline
New Member
 
Join Date: Nov 2008
Posts: 1
gadisandeep is on a distinguished road

Exception in thread "main" java.util.NoSuchElementException


hi this is my code.I am reading from a .csv which contains names in 1st row and 1st column and number in between. i am trying to read that into a two dimensional array but its thrwing this error.
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
at readcsv.readCSV.main(readCSV.java:45)
my codes is

JAVA Code:
package readcsv;

import java.io.*;
import java.net.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;

public class readCSV 

{ //readCSV class starts here 

public static void main(String args[]) throws IOException 

{ //main method starts 
 String[][] Data= new String[20][20];
 int row = 0;
 int col = 0;
String fName = "C:/Users/sgadi/Desktop/combined_one.csv";//csv file which u wanna read 

String thisLine; //string variable which take each record at a time

int count=0; //to count no. of records

FileInputStream fis = new FileInputStream(fName); 
//A FileInputStream obtains input bytes from a file in a file system

DataInputStream myInput = new DataInputStream(fis);
/*data input stream lets an application read primitive Java data types 
from an underlying input stream in a machine-independent way*/

while ((thisLine = myInput.readLine()) != null)
{ //beginning of outer while loop 
StringTokenizer st =new StringTokenizer(thisLine,",");
while(st.hasMoreElements()){
    
String field = st.nextToken();
System.out.print(field+", ");
Data[row][col] = st.nextToken();
col++;
System.out.println(col);
}
row++;

System.out.println();
System.out.println(Data[row][col].toString());
System.out.println(col);
count++;
System.out.println(count);
} //ending of outer while loop 
} //main method ends 
} //readCSV class ends here
Last edited by LuciWiz : 20-Nov-2008 at 01:15. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 19-Nov-2008, 18:45
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: geetting this error Exception in thread "main" java.util.NoSuchElementException


Out of curiosity, what is your java version?

I get this compiling the code:
Code:
1. WARNING in readCSV.java (at line 30) while ((thisLine = myInput.readLine()) != null) ^^^^^^^^^^^^^^^^^^ The method readLine() from the type DataInputStream is deprecated
Do you know how far into the CSV file the read is happening?

Anyway, here's what the Java API says about DataInputStream's readline() call:
Code:
public final String readLine() throws IOException Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form: DataInputStream d = new DataInputStream(in); with: BufferedReader d = new BufferedReader(new InputStreamReader(in));
So consider using the BufferedReader class, as suggested above, or (depending on your Java version) the Scanner class.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 30-Nov-2008, 07:42
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Exception in thread "main" java.util.NoSuchElementException


Quote:
Originally Posted by gadisandeep
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
at readcsv.readCSV.main(readCSV.java:45)

JAVA Code:
StringTokenizer st =new StringTokenizer(thisLine,",");
while(st.hasMoreElements()){
    
String field = st.nextToken();
System.out.print(field+", ");
Data[row][col] = st.nextToken();
col++;
System.out.println(col);
}
row

If you happen to see the StringTokenizer Loop, you are using st.nextToken() two times.!!!!!
That is the cause of the error.
Whenever you call a nextToken() method, the tokenizer will automatically move on the next item.

Here is an example:
Suppose that your file has the following items:
1,2,3,4,5,6

When you try out your code, it will print 1, 3, 5, but actually, only 2, 4 and 6 are added to your data.

The solution to your problem might be:
JAVA Code:
StringTokenizer st =new StringTokenizer(thisLine,",");
while(st.hasMoreElements()){
    
String field = st.nextToken();
System.out.print(field+", ");

// Use the token which you obtained. Do not get a new Token
Data[row][col] = field;
col++;
System.out.println(col);
}
row
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 30-Nov-2008, 14:30
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Exception in thread "main" java.util.NoSuchElementException


Thanks for pointing that out, Paramesh, I completely overlooked that double call!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.lang.NullPointerException valley1girl Java Forum 12 18-Jun-2009 12:32
Exception in thread "main" java.lang.NullPointerException gedcraw555 Java Forum 3 25-Feb-2008 08:22
Exception in thread "main" java.lang.NullPointerException Magelloo Java Forum 3 05-May-2007 23:01
Thread synchronization Assignment help!! rossi143 C Programming Language 1 25-Mar-2007 20:10
please help in solving: Exception in thread "main" java.lang.nullpointerexception vkiran_v Java Forum 8 09-May-2006 07:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:10.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.