EditText to Int
You approach of
final EditText height = (EditText) findViewById (R.id.height);
String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
is correct, but you are calling this directly in your onCreate
method. This means they get called as soon as the app starts, even before the user is able to do anything. because of this the values are always empty, simply because the user hasn't even had the change to do anything yet.
When you use the values later in your onClick
(which is correct), they are still empty due to their early initialization.
Try putting this part:
String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
Directly after the start of your onClick
method but before your if/else
statements like so:
public void onClick(View v) {
String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
// Your statements
}
granra
Updated on June 04, 2022Comments
-
granra almost 2 years
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:
final EditText height = (EditText) findViewById (R.id.height); String heightString = height.getText().toString(); final int heightInt = Integer.parseInt(heightString);
and
final EditText height = (EditText) findViewById (R.id.height); final int heightInt = Integer.parseInt(height.getText().toString());
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.
Here's the whole code:
package com.body.calculator;
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("") && 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(""); } } }); } }
Any help would be appreciated. Thanks, Arnar.
-
granra over 12 yearsThanks, should have seen that :/ However, now it force closes when I press the submit button.
-
granra over 12 yearsI put the code in Try-Catch and it outputs "java.lang.NumberFormatException: unable to parse ' ' as integer" when the edittexts are empty (as expected) but I'm unable to produce any error if I put something in the EditTexts.
-
granra over 12 yearsI'm sorry for spamming the comments but I managed to find what is force closing it now. If I set results.setText("no force-close"); results change to "no force-close" but if I set results.setText(heightInt); I get a force close. What could be the problem?
-
Sander van't Veer over 12 yearsDid you check your LogCat after you received the error? Most likely heightInt is empty when you try to set the result, resulting in a null pointer which force-closes your app.
-
granra over 12 yearsIt worked after I converted the int to string and put that in the setText(). However, now I am facing another problem. For this formula to work height has to be in cm. If I put commas in the EditText I get error about that Int doesn't like that. So if I put mm (180mm for example) and try to convert it to cm (180/100) I only get 1 not 1.8. What other way could I get mm to cm?
-
Sander van't Veer over 12 years
Integers
are round numbers. They are always rounded down. What you are looking for, aredoubles
. These take fragmented numbers such as 1,8. Try usingdoubles
for you calculation. -
granra over 12 yearsOkay, I've got this now. Thank you very much for your help.
-
Saeed Zhiany over 4 yearsWelcome to SO, please avoid putting some code without any explanation.