Really about to rip my hair our here. I've been trying to get very simple programs to work in Java in Android Studio a whole day which I have no problem doing in other languages and IDEs. I've followed multiple tutorials just trying to get simple programs to work. I'm stuck with an Fatal Exception error saying java.lang can't convert a string to a float.
Heres my code, its one page thats just trying to take a float, multiply it by another float then add the product to the first float, I left in some attempts to "fix" the problem so if they're look bad its because I'm trying everything:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText priceText, taxText, finalText;
Button button;
float price = 0.00f;
float tax = 0.0f;
float finalprice = 0.00f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
priceText = findViewById(R.id.priceText);
priceText.setText(String.valueOf(price));
taxText = findViewById(R.id.taxText);
String str = tax "%";
taxText.setText(str);
finalText = findViewById(R.id.finalText);
finalText.setText(String.valueOf(finalprice));
button = findViewById(R.id.calcButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//String p = priceText.getText().toString();
price = convertStringToFloat(priceText.getText().toString());
//String t = taxText.getText().toString();
tax = Float.parseFloat(String.valueOf(taxText));
float _taxes = price * tax;
finalprice = price _taxes;
finalText.setText(String.valueOf(finalprice));
}
});
}
public static float convertStringToFloat(String str)
{
return Float.parseFloat(str);
}
}
This what the Console keeps spitting out when the App crashes:
2021-09-07 10:15:34.700 7772-7772/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7772
java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{53d2c07 VFED..CL. .F...... 312,404-762,551 #7f08019b app:id/taxText aid=1073741826}"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at com.example.myapplication.MainActivity$1.onClick(MainActivity.java:42)
at android.view.View.performClick(View.java:7161)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7133)
at android.view.View.access$3500(View.java:804)
at android.view.View$PerformClick.run(View.java:27416)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:7617)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2021-09-07 10:15:34.728 7772-7772/com.example.myapplication I/Process: Sending signal. PID: 7772 SIG: 9
CodePudding user response:
You appear to be failing here:
tax = Float.parseFloat(String.valueOf(taxText));
taxText
is an EditText
widget:
EditText priceText, taxText, finalText;
If you are trying to get the value that the user typed in, replace String.valueOf(taxText)
with taxText.getText().toString()
.
Note that the user might still type in a value that is not a valid floating-point number, so you will still want to wrap your parseFloat()
call in a try
/catch
and do something in the case you get invalid input (e.g., log a message to Logcat via Log.e()
and provide some error feedback to the user).