我試圖將2d字符串數組轉換爲2d double數組。數組的長度對於每一行都是不同的,所以我無法將數組轉換爲double。我得到一個空指針異常錯誤。文件看起來像這樣將2d字符串數組轉換爲不同長度的2d double
1.0,0.0,0.0,0.456
0.0,1.0,0.0,0.725
0.0,0.0,1.0,-0.150
public class first {
public static void main(String[] args) throws FileNotFoundException {
java.io.File test2 = new java.io.File("object.key");
Scanner input = new Scanner(test2);
String arr[][]=new String[30][30];
int i=0,j=0;
while(input.hasNext())
{
String val=input.nextLine();
j=0;
if(val.contains(" "))
{
String str[]=val.split(" ");
int cn=str.length;
while(cn>0)
{
arr[i][j]=str[j];
cn--;
j++;
//System.out.println(cn);
}
}
else
arr[i][j]=val;
i++;
}
double[][] intarray=new double[arr.length][arr.length];
int kk=0,jj=0;
for(kk = 0; kk < 16; kk++)
{
for(jj=0;jj <arr.length; jj++)
{
intarray[kk][jj] = Double.parseDouble(arr[kk][jj]);
System.out.println(intarray[kk][jj]);
}
}
}
}
謝謝
什麼行會導致NullPointerException? – 2013-04-20 05:54:27
它讀取前兩個數字5和67,然後它指向NullPointerException。謝謝 – user2109988 2013-04-20 06:03:54