Skip to content Skip to sidebar Skip to footer

Number Format Exception In Reading File Which Has Arraylists

I receive this error in the title. I create 2 arraylists from edit text fields and save them in an xls file. To save: File sdCard = Environment.getExternalStorageDirectory(); File

Solution 1:

The code that saves data only stores a text line containing both number separates by a tab. When your code reads the file, you read the entire line, trying to parse the text, that obviously is unparseable (has a tab).

You should iterate your arrays writing each line. Then, the read method should split the readed string and parse each substring separately.

Regards.

Lets try to explain what I would do [not compiled]:

FilesdCard= Environment.getExternalStorageDirectory();
Filedirectory=newFile (sdCard, "MyFiles");
directory.mkdirs();            
Filefile=newFile(directory, filename);


FileOutputStream fos;

try {
    fos = newFileOutputStream(file);
    BufferedWriterbw=newBufferedWriter(newOutputStreamWriter(fos));
    //I assume mydata and myweight are both List<Double>... with same sizefor(inti=0; i < mydata.size(); i++){
        bw.write(mydata.get(i)+"\t"+myweight.get(i)+"\n");
    }        
    bw.flush();
    bw.close();
} catch (IOException e2) {
    e2.printStackTrace();
}//catch

...

File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
File file = new File(directory, filename);

String s;
FileInputStream fis;

try {
    fis = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    do {
        s = br.readLine();
        if (s != null)
        {
            String[] splitLine = s.split("\\t");
            data.add(Double.parseDouble(splitLine[0]));
            weight.add(Double.parseDouble(splitLine[1]));
        }
    } while (s != null);
} catch (IOException e) {
     e.printStackTrace();
}

Post a Comment for "Number Format Exception In Reading File Which Has Arraylists"