0

i try to read text from screen and change it to double and it crash

public void equesionOperation(int signNum1) {

        S_numInTV=TV_calcScreen.getText().toString();
        S_numUp=TV_calcUp.getText().toString();

        D_numIn=Double.parseDouble(S_numInTV);
        D_numToCalc=Double.parseDouble(S_numUp);

        switch (signNum1){
        case 1: D_sum=D_numIn+D_numToCalc;break;
        case 2: D_sum=D_numIn-D_numToCalc;break;
        case 3: D_sum=D_numIn*D_numToCalc;break;
        case 4: D_sum=D_numToCalc/D_numIn;break;
        case 5: D_sum=Math.pow(D_numToCalc, D_numIn);break;
        default: break;
        }
        S_sum=(""+D_numToCalc+"  "+D_numIn);
    }
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • 2
    It would help if you elaborated on *"it crash"*... Does it throw an exception? which exception? what are the inputs? – assylias Aug 26 '12 at 08:48
  • 1
    Side comment: you should also read [that post about hungarian notation](http://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation). – assylias Aug 26 '12 at 08:49
  • in particular, read this: http://www.joelonsoftware.com/articles/Wrong.html – sschrass Aug 26 '12 at 09:08

2 Answers2

0

This may help to find the cause of the crashes. Replace the 3rd and 4th lins of code by:

try {
   D_numIn=Double.parseDouble(S_numInTV);
} catch (NumberFormatException nfe) {
   System.out.printf("Attempted to parse a double, but found: '%s'%n", S_numInTV);
}
try {
D_numToCalc=Double.parseDouble(S_numUp);
} catch (NumberFormatException nfe) {
   System.out.printf("Attempted to parse a double, but found: '%s'%n", S_numUp);
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0
 S_numInTV=TV_calcScreen.getText().toString();
 S_numUp=TV_calcUp.getText().toString();

these string value is literal or numeric?

String str1 = "100.476";
Double dObj2 = Double.valueOf(str1);
System.out.println(dObj2);

the above code will not occur any exception but

String str1 = "ss";
    Double dObj2 = Double.valueOf(str1);
    System.out.println(dObj2);

the above code will occur numberfomatexception

So before converting string to double you have to ensure which string is inputting;

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37