Note that there are some explanatory texts on larger screens.

plurals
  1. POEditText to Int
    text
    copied!<p>I'm new to Java and Android development so I decided to make a simple app that takes in two fields and puts them in a simple math equation. However I have to convert the EditText into Int before I can do any math with them, right? In the XML file I have set both EditTexts to android:inputType="number". My attempts have been like this:</p> <blockquote> <pre><code> final EditText height = (EditText) findViewById (R.id.height); String heightString = height.getText().toString(); final int heightInt = Integer.parseInt(heightString); </code></pre> </blockquote> <p>and</p> <blockquote> <pre><code> final EditText height = (EditText) findViewById (R.id.height); final int heightInt = Integer.parseInt(height.getText().toString()); </code></pre> </blockquote> <p>I also tried Integer.valueOf in both cases but it always force closes my app when I run it and if I comment out that int heightInt it doesn't force close.</p> <p>Here's the whole code:</p> <blockquote> <p>package com.body.calculator;</p> <pre><code>import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Vibrator; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class bmi extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView results = (TextView) findViewById(R.id.results); final EditText height = (EditText) findViewById (R.id.height); String heightString = height.getText().toString(); final int heightInt = Integer.parseInt(heightString); final EditText weight = (EditText) findViewById (R.id.weight); final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); Button submit_bmi = (Button) findViewById(R.id.submit_bmi); submit_bmi.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (height.getText().toString().equals("") &amp;&amp; weight.getText().toString().equals("")) { results.setText("Both fields are empty"); vib.vibrate(100); } else if (height.getText().toString().equals("")) { results.setText("Height is empty"); vib.vibrate(100); } else if (weight.getText().toString().equals("")) { results.setText("Weight is empty"); vib.vibrate(100); } else { results.setText(""); } } }); } } </code></pre> </blockquote> <p>Any help would be appreciated. Thanks, Arnar.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload