Why Am I not able to convert String (taken from EditText) to Float.
I was building a simple app to calculate (addition, subtraction, division, multiplication) of two numbers as per the radio group selection. But, As I extract string using
String temp = editText.getText().toString();
and then try to convert it back into Float in order to perform operation on it
num1 = Float.parseFloat(temp);
This very line of code causes some error which crashes my app. I tried to surround it with try-catch which prevented crashing of my app but String was still not parsing into Float.
Complete Java Code for Calculator App
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
float ans;
EditText editText;
TextView textView;
float num1, num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.first_number);
String temp = editText.getText().toString();
try{
num1 = Float.parseFloat(temp);
}catch(Exception e){
Log.e("MainActivity", "onCreate: Still Not Working", e);
}
editText = findViewById(R.id.second_number);
try{
num2 = Float.parseFloat(editText.getText().toString());
}catch(Exception e){
Log.e("MainActivity", "onCreate: String unable to Assign.", e);
}
textView = findViewById(R.id.answer);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
caluclate(num1, num2);
if(num2 == 0 && checkedId == R.id.div){
textView.setText("N.A.");
}else{
textView.setText(String.valueOf(ans));
}
}
});
}
float addition(float n1, float n2){
return n1 n2;
}
float subtraction(float n1, float n2){
return n1 - n2;
}
float multiplication(float n1, float n2){
return n1 * n2;
}
float division(float n1, float n2){
return n1/n2;
}
void caluclate(float n1, float n2){
switch(radioGroup.getCheckedRadioButtonId()){
case R.id.add:
ans = addition (n1, n2);
break;
case R.id.sub:
ans = subtraction(n1, n2);
break;
case R.id.mul:
ans = multiplication(n1, n2);
break;
case R.id.div:
ans = division(n1, n2);
break;
}
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DFDFDF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="First Number"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@ id/first_number"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:hint="Enter Here"
android:inputType="numberDecimal|numberSigned"
android:padding="8dp"
android:background="@color/white"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Second Number"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@ id/second_number"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:hint="Enter Here"
android:padding="8dp"
android:inputType="numberDecimal|numberSigned"
android:background="@color/white"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Choose Operation"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="@ id/radio_group"
android:paddingLeft="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@ id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Addition" />
<RadioButton
android:id="@ id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Subtraction" />
<RadioButton
android:id="@ id/mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Multiplication" />
<RadioButton
android:id="@ id/div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Division" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Answer"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@ id/answer"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:textSize="20dp"
android:padding="8dp"
android:inputType="number"
android:background="@color/white"
android:textStyle="bold" />
</LinearLayout>
My app was crashing until I comment out that line which assigns String to float then I tried using try-catch block but this just prevented my app from crashing
CodePudding user response:
here you go
you should get value from edittext at runtime when action perform,not at creating activity.
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
float ans;
EditText editText1,editText2;
TextView textView;
float num1, num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make sure that you entered value contains float,not other data type
editText1 = findViewById(R.id.first_number);
editText2 = findViewById(R.id.second_number);
textView = findViewById(R.id.answer);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
try{
num1 = Float.parseFloat(editText1.getText().toString()); //i'm getting value here
}catch(Exception e){
Log.e("MainActivity", "onCreate: Still Not Working", e);
}
try{
num2 = Float.parseFloat(editText2.getText().toString());//i'm getting value here
}catch(Exception e){
Log.e("MainActivity", "onCreate: String unable to Assign.", e);
}
caluclate(num1, num2);
if(num2 == 0 && checkedId == R.id.div){
textView.setText("N.A.");
}else{
textView.setText(String.valueOf(ans));
}
}
});
}
float addition(float n1, float n2){
return n1 n2;
}
float subtraction(float n1, float n2){
return n1 - n2;
}
float multiplication(float n1, float n2){
return n1 * n2;
}
float division(float n1, float n2){
return n1/n2;
}
void caluclate(float n1, float n2){
switch(radioGroup.getCheckedRadioButtonId()){
case R.id.add:
ans = addition (n1, n2);
break;
case R.id.sub:
ans = subtraction(n1, n2);
break;
case R.id.mul:
ans = multiplication(n1, n2);
break;
case R.id.div:
ans = division(n1, n2);
break;
}
}
}