1

I run my app in the emulator and everything works fine, but when I run it in my device ( Huawei u8650) I get NumberFormatException:

GraphViewData[] aux2 = new GraphViewData[aux.length];
     for(int i=0;i<aux.length;i++)
     {
         System.out.println("reps "+Repetitions+", dato "+i+" = "+aux[i]);
         if(aux[i] != null)
         {
             try{
         aux2[i]= new GraphViewData(i, Double.parseDouble(aux[i]));
             }catch(Exception w)
         {
                 Toast.makeText(this, "num: "+aux[i], Toast.LENGTH_LONG).show();
                 Toast.makeText(this, w.getMessage(), Toast.LENGTH_LONG).show();
         }
         }
}

The exception is at : aux2[i]= new GraphViewData(i, Double.parseDouble(aux[i])); OK, you would think that aux[i] would have an incorrect format to parse to double but all are number like this: 70.5 or like this 84 So I don´t know why it gives that exception only in real device but not in emulator.

Any suggestions? thanks in advance.

Kara
  • 6,115
  • 16
  • 50
  • 57
J. Arenas
  • 493
  • 1
  • 9
  • 23

2 Answers2

0

I is caused by different locale, which specifies different decimal separator than you use (usually it expects , and you use .). Just replace any , with . prior calling parseDouble() and you will be fine.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I cannot do that beacause this app is in different languages, for example in English you have to use dot and in Spanish you have tu use comma – J. Arenas Sep 10 '12 at 10:56
0

OK, the answer is using this code:

public double stringToDouble(String s) {

       NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
    nf.setGroupingUsed(false);
    ParsePosition parsePosition = new ParsePosition(0);
    Number n = nf.parse(s, parsePosition);
    if (n == null || parsePosition.getErrorIndex() >= 0 || parsePosition.getIndex() < s.length())
    {
      /* not a valid number */
    }
    return n.doubleValue();
}

This works for me

J. Arenas
  • 493
  • 1
  • 9
  • 23