Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String to Double in Android

Trying to get double values from an EditText and manipulate them before passing them to another Intent. Not using primitive data type so I can use toString methods.

Problem is when I include the protein=Double.valueOf(p).doubleValue(); style commands, the program force closes immediately without leaving any info in the logcat.If I comment them out and set some dummy data like protein = 1.0; it works with no problems. Same happens with primitive data types and parse double. This code works perfectly with dummy data in normal java. What am I doing wrong?

EditText txtProt, txtCarb, txtFat, txtFiber, txtPoints; String p, c, f, fi; Double protein, carbs, fat, fiber; double temp; Integer points;  @Override public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      Log.v("Create Prompt", "ready for layout");      setContentView(R.layout.main);      Log.v("Layout Created", "ready for variable assignment");      txtProt = (EditText) findViewById(R.id.Protein);      txtCarb = (EditText) findViewById(R.id.Carbs);      txtFat = (EditText) findViewById(R.id.Fat);      txtFiber = (EditText) findViewById(R.id.Fiber);      txtPoints = (EditText) findViewById(R.id.Points);      btnCalc = (Button) findViewById(R.id.Calc);      Log.v("Variables Assigned", "ready for double assignment");       p = txtProt.getText().toString();      c = txtCarb.getText().toString();      f = txtFat.getText().toString();      fi = txtFiber.getText().toString();        protein=Double.valueOf(p).doubleValue();      carbs=Double.valueOf(c).doubleValue();      fat=Double.valueOf(f).doubleValue();      fiber=Double.valueOf(fi).doubleValue();      Log.v("Doubles parsed", "ready for calculations");      //these are the problem statements       protein = 1.0;      carbs = 1.0;      fat = 1.0;      fiber = 1.0;       protein *= 16;      carbs *= 19;      fat *= 45;      fiber *= 14;       temp = protein + carbs + fat - fiber;      temp = temp/175;       points = new Integer((int) temp); 
like image 442
ProfCommie Avatar asked Jul 28 '11 22:07

ProfCommie


People also ask

How do I convert a String to a double in Kotlin?

Kotlin convert String to DoubletoDouble() to parse the string to a Double , NumberFormatException is thrown if the string is not a valid representation of a Double. toDoubleOrNull() to convert the string to a Double , return a null if the string is not a valid representation of a Double.

What is double in Android?

double : a double precision floating-point number. the bits that represent the floating-point number.


1 Answers

I would do it this way:

try {   txtProt = (EditText) findViewById(R.id.Protein); // Same   p = txtProt.getText().toString(); // Same   protein = Double.parseDouble(p); // Make use of autoboxing.  It's also easier to read. } catch (NumberFormatException e) {   // p did not contain a valid double } 

EDIT: "the program force closes immediately without leaving any info in the logcat"

I don't know bout not leaving information in the logcat output, but a force-close generally means there's an uncaught exception - like a NumberFormatException.

like image 167
Izkata Avatar answered Oct 15 '22 04:10

Izkata