运行java程序时出错 [英] error running java program
本文介绍了运行java程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
程序是将程序生成的坐标写入另一个文件,然后读取文件。我的程序编译但是当我运行它时我得到这个错误
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:
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
推荐答案
请始终注释异常所在的行抛出。
看起来你试图解析字符串[29.42051539113255,-1.3622522059837383],
表示坐标对(和其他东西,','),整数(Integer.ParseInt
)。您应该1)首先将此字符串解析为两个代表两个数字的子字符串; 2)解析表示小数小数的其他数字类型,例如float
或double
。通讯员readarra
应该是一个相同类型的数组,而不是int
。
更重要的是,你的函数readfile
什么都不返回。退出此方法时,对象readarray
将丢失。我已经在你对你的另一个问题的评论中告诉你,你应该返回int []
类型,你为什么忽略我的建议? (考虑到你的文件格式,它可能应该是double []
。)
此外,我会质疑将一些文件解析为数值数组的整体思路。现在,初学者展示了一种非常糟糕的趋势,即使用表示数据的字符串而不是数据本身。但是,我不确定这个谬论是否与您的案件有关。试想一下。
-SA
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 asfloat
ordouble
. Correspondentreadarra
should be an array of the same type, notint
.
More importantly, your functionreadfile
returns nothing. The objectreadarray
will be lost on exit of this method. I already told you in my comment to your other question that you should returnint[]
type, why did you ignore my advise? (Considering your file format, it probably should bedouble[]
.)
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
这篇关于运行java程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文