Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The program is to write the coordinates generated by the program to another file, then read the file. My program compiles but when i run it i get this error

java.lang.NumberFormatException: For input string: "[29.42051539113255, -1.3622522059837383], "
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at J4.readfile(J4_2_MultiDimensionalArray7.java:52)
	at J4.main(J4.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>


What does this error mean? How can I fix it? Thanks!

CODE:

Java
import java.io.*;
import java.util.Arrays;

public class J4
{
    public static void main (String [] args) throws IOException
    {
        int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100
        
        //arrays are initializewd and declared
        double [] lengthscale = new double [dimension];
        double [][] locations = new double [numpoints][dimension];
        
        PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));
        
        //calls writefile method
        writefile(lengthscale, locations, dimension, numpoints, length);
        
        for(int m=0; m <length; m++){//for loop
          fileOut.println(Arrays.toString(locations[m]) + ", ");//writes to file
        }
        fileOut.close ();//close file
        
        //calls readfile method
        readfile();
    }//end main
    
    public static void writefile(double lengthscale[], double locations[][], int dimension, int numpoints, int length)throws IOException
    {
    
        for (int a = 0; a < dimension; a++) {// for loop runs while a is less than dimension
            lengthscale[a] = length;// stores array
        }// end for loop
        
        for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
            for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
                locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and chooses random point within 
                
            }//end nested for loop
        }//end for loop
    }//end writefile method
    
    public static void readfile()throws IOException
    {
        BufferedReader readFile = new BufferedReader (new FileReader ("arrayNumPoints.txt"));
        
        //initialize array
        int readarray [] = new int [100];
        //for loop runs while counter is less than 100
        for (int counter=0; counter < 100; counter++){
            //reads file
            readarray[counter] = Integer.parseInt (readFile.readLine ());
        }//end for loop
        
    }//end readfile method
}//end class
Posted
Updated 23-Sep-13 21:42pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-13 21:44pm    
In what line?
—SA
Member 10292973 23-Sep-13 21:58pm    
Im not sure because when I compile it, it shows no errors. But when I run it, that warning comes up on the interactions pane. Do you know what it means?
Sergey Alexandrovich Kryukov 23-Sep-13 22:04pm    
What error?! This is exception, happens during run-time? Haw can you possibly not know it?! How about the debugger? You already have line number in your exception stack, all you need is to comment it in your code...
—SA
Sergey Alexandrovich Kryukov 23-Sep-13 22:04pm    
What error?! This is exception, happens during runtime. Do you really think that "no errors" means correct code? :-)
How can you not know where the exception is thrown?! You have the whole exception stack trace, with line numbers. All you had to do is to comment it in your code.

I answered, anyway...

—SA
Richard MacCutchan 24-Sep-13 3:43am    
You create an array of double values which you write to the file, and then try to read them back in as integers. You must read them back in the same format as you wrote them.

1 solution

Please, always comment the line where the exception is thrown.

It looks like you try to parse the string "[29.42051539113255, -1.3622522059837383], " representing a coordinate pair (and something else, ','), as integer (Integer.ParseInt). You should 1) first parse this string into two sub-strings representing two numbers; 2) parse other numeric types representing fractional decimals, such as float or double. Correspondent readarra should be an array of the same type, not int.

More importantly, your function readfile returns nothing. The object readarray will be lost on exit of this method. I already told you in my comment to your other question that you should return int[] type, why did you ignore my advise? (Considering your file format, it probably should be double[].)

Besides, I would question the whole idea of parsing of some file into numeric array. These days, the beginners demonstrate a really bad trend to work with strings representing data instead of data itself. However, I'm not sure if this fallacy related to your case or not. Just think about it.

—SA
 
Share this answer
 
v2
Comments
Prasad Khandekar 24-Sep-13 3:47am    
My 5+
Sergey Alexandrovich Kryukov 24-Sep-13 8:55am    
Thank you, Prasad.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900