Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.NumberFormatException after use of StringTokenizer

807580Feb 9 2010 — edited Feb 10 2010
Hi !
I've got a problem of java.lang.NumberFormatException when trying to convert a String to a double using Double.parseDouble(). The String is read from a text file generated with Excel. I parsed the text file and put the strings in a String[][] using StringTokenizer. I manage to print the strings without any problem, and their format seems correct.
I wonder if the problem comes from the fields (count, hash, offset , value) that can be seen in the debugger.
I've compared to codes found on the internet and cannot find any explanation.

I'm using Eclipse 3.4.1 under Mac OS 10.5.7. The JRE is JVM 1.5.0.

Thank you very much !


Here is the code (it's part of a bigger project, so I just put what is relevant here):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class lecture{

	public static void main(String[] args){

		try{
			int row = 0;
			int col = 0;
			int count = 0;
			String line = null;
			String Path_ressource = "../lecture_fichier_texte/ressources/";  //lecture_fichier_texte is the name of the project

			File file = new File(Path_ressource+"Caribou_C1"  +".txt");
			BufferedReader bufRdr = new BufferedReader(new FileReader(file));
			//go through the text file to count the number of lines
			while((line = bufRdr.readLine()) != null ){
				count++;
			}

			//set localisation's size to be the same as the number of line of the text file 
			String[][] localisation = new String [count][5];

			bufRdr.close();

			bufRdr = new BufferedReader(new FileReader(file));
			//read each line of text file
			while((line = bufRdr.readLine()) != null){
				StringTokenizer st = new StringTokenizer(line,";",false);

				while (st.hasMoreTokens()){
					//get next token and store it in the array
					localisation[row][col] = st.nextToken(";");		
					col++;
				}
				col = 0;
				row++;
				line = bufRdr.readLine();
			}	
			bufRdr.close();

			//recover the values in the array and use them as double values
			int i;
			for (i=1;i<=count;i++){
				String Xstr1 =localisation[3];
System.out.println(Xstr1);

//just a test : localisation[1][3] should be equal to "497604.122" but temp stays at 0
String Xstr ="497604.122";
int temp =0;
if (Xstr.equals(Xstr1)){
temp=1;
}

//try to convert the string into a double value
double x_localisation = Double.parseDouble(Xstr1);
}

}
catch (IOException e){
e.printStackTrace();
}
}

}
and the textfile, named "Caribou_C1.txt" :

CARIBOU;DATE;HOUR;X_PROJ;Y_PROJ
CA12;20-mars-05;1;497604.122;5624494.493
CA12;20-mars-05;5;497606.0709;5624439.115
CA12;20-mars-05;9;497503.6775;5624569.711
CA12;20-mars-05;13;497590.4344;5624476.93
CA12;20-mars-05;17;497595.5124;5624479.263
CA12;20-mars-05;21;497339.8814;5624159.689
CA12;21-mars-05;1;497339.0382;5624165.694
CA12;21-mars-05;5;497342.2094;5624161.467
CA12;21-mars-05;9;497622.4391;5624459.124                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 10 2010
Added on Feb 9 2010
22 comments
1,371 views