java - converting 2d string array into 2d double with different length -


i trying convert 2d string array 2d double array. length of array different each line, unable convert array double. getting null pointer exception error. file this

5 67

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]);        }    } } } 

thank you

the problem trying parsedouble null value.
happens because while declared array of [30][30], elements have not been initialized in upper loop null.

if not sure element initialized, in second loop check , avoid null values:

for(jj=0;jj <arr.length; jj++) {        if (arr[kk][jj] != null) intarray[kk][jj] = double.parsedouble(arr[kk][jj]);        system.out.println(intarray[kk][jj]); } 

while not optimized, @ least initialized values processed (then you'll have check upper loop , file ensure numeric values non-null elements).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -