0

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 directory = new File (sdCard, "MyFiles");
directory.mkdirs();            
File file = new File(directory, filename);


FileOutputStream fos;


try {
   fos = new FileOutputStream(file);
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
   bw.write(mydata+"\t"+myweight+"\n");
   bw.flush();
   bw.close();
    } catch (IOException e2) {
       e2.printStackTrace();
        }//catch

and to load :

       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)
                     {
                     data.add(Double.parseDouble(s));
                     weight.add(Double.parseDouble(s));
                     }
                 } while (s != null);
           } catch (IOException e) {
              e.printStackTrace();
               }
    }

It shows the number format exception in line "data.add(Double.parseDouble(s));".

Also, I enter for example in the 2 fields 1,10 and 2,20 and the file contains a cell which has [1.0, 2.0] [1.0, 2.0]

The other data?And how to put the data in 2 columns?

George
  • 5,808
  • 15
  • 83
  • 160
  • Post the entire log cat please. – TJ Thind Apr 15 '13 at 21:32
  • You should add a try/catch around the calls to Double.parseDouble() because (as you noticed), parseDouble throws NumberFormatException. By add the try/catch, you will be able to print the value of s and see what is wrong. E.g. try { double x = Double.parseDouble(s); } catch (NumberFormatException ex) { Log.d("Double.parseDouble", "NumberFormatException, s=" + s); }. – Yojimbo Apr 15 '13 at 23:42
  • @Yojimbo:It just gives "NumberFormatException, s=[1.0, 2.0]" and "NumberFormatException, s=[1.0, 2.0]". It seems that it can't save the data from the "weight" field.. – George Apr 16 '13 at 07:29

1 Answers1

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]:

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


FileOutputStream fos;

try {
    fos = new FileOutputStream(file);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    //I assume mydata and myweight are both List<Double>... with same size
    for(int i = 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();
}
eltabo
  • 3,749
  • 1
  • 21
  • 33
  • You mean that? bw.write(mydata+"\n"); bw.write(myweight+"\n"); .Because it still has the same results.It seems that it can't save the "weight" field. – George Apr 16 '13 at 07:32
  • :Thanks!It worked great!I used ArrayList but now I use List.Now in the excel file it is in one cell for example "1 10" in the below cell "2 20".I want to have the data in 2 columns."data" in the 1st and weight in the 2nd.I can't figure how to do it. – George Apr 16 '13 at 08:48
  • Well, actually you're using a simple plain text not a xls file. It isn't trivial dealing with excel file in Java (see Apache POI), but you can save your data as CSV, by using comma as separator. Excel knows how to dealing with and show each data in different column – eltabo Apr 16 '13 at 09:19
  • Use a SimpleDateFormat to parse The date using The same format pattern you use to store it. – eltabo Apr 16 '13 at 09:41
  • :Hello, if you can help me in the chat i'll appreciate it.I have a problem with the " data.add(Double.parseDouble(splitLine[0]));"line because i have Date and also I can't figure the csv formta for 2 columns.If you can't it's ok.Thanks! – George Apr 16 '13 at 16:46
  • :Actually I managed the csv , I use s.split(","); and bw.write(mydata.get(i)+","+myweight.get(i)+"\n") and it stores to 2 columns. I can't manage the Date format though,I don't know how to handle it.Also, even if I delete the file from the folder it still keeps the previous data!!Can you help me? – George Apr 16 '13 at 17:06
  • Please check here if you want..Sorry to disturb I just can't figure.. http://stackoverflow.com/questions/16014103/achartengine-cant-figure-how-to-use-dates-as-x-axis-the-file-i-save-is-empt . I have problem with the Date format.I receive an empty file! – George Apr 16 '13 at 18:06