android - Error java.lang.NumberFormatException : For input string, program stops working

  • Thread starter Thiru Navakarasu
  • Start date
T

Thiru Navakarasu

Guest
This is how the code looks, at first it was running properly but after adding new functions of buttons, this segment of code didn't run properly like before. Please help me I have submission on Wednesday this week. Is is possible that is this is a bug?

if(mSinH){
if(mArithmetic){
try {
String str;
str = txtbtn.getText().toString();
str = str.substring(4);
double aa = 0;
aa = Math.asin(Double.parseDouble(str));
txtbtn.setText(aa + "");
mArithmetic = false;
mSinH = false;
}
catch(Exception e){
//
}
}
}
if(mCosH){
if(mArithmetic){
try {
String str;
str = txtbtn.getText().toString();
str = str.substring(4);
double aa = 0;
aa = Math.acos(Double.parseDouble(str));
txtbtn.setText(aa + "");
mArithmetic = false;
mCosH = false;
}
catch(Exception e){
//
}
}
}
if(mTanH){
if(mArithmetic){
try {
String str;
str = txtbtn.getText().toString();
str = str.substring(4);
double aa = 0;
aa = Math.atan(Double.parseDouble(str));
txtbtn.setText(aa + "");
mArithmetic = false;
mTanH = false;
}
catch(Exception e){
//
}
}
}


I expected the output of arc_sin 1 to be 1.570796327 as exactly from my actual scientific calculator but the program crashed every time. Below is the source code from my program:

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity{
TextView txtbtn;
float mValueOne, mValueTwo;
double a;
double ans=0;
boolean mAddition, mSubtract, mMultiplication, mDivision, mArithmetic, mReminder,
power2nd, mSin, mCos, mTan, mSinH, mCosH, mTanH;
ImageButton deletebutton;
Button clearbutton, dividebutton, multiplybutton, button7, button8, button9, minusbutton,
button4, button5, button6, plusbutton, button1, button2, button3, button0, pointbutton,
equalbutton, xcubebutton, squarebutton, modulusbutton, exponentbutton, tenexponentbutton,
piebutton, squarerootbutton, cuberootbutton, stpower2nd, xfactorialbutton, logbutton, lnbutton,
sinbutton, cosbutton, tanbutton, sinhbutton, coshbutton, tanhbutton, oneoverxbutton, absbutton,
floorbutton, expowerbutton;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);

scientific_operation();

}
I created void function here to store all the buttons used in the program.

public void scientific_operation(){
txtbtn = findViewById(R.id.txt);
deletebutton = findViewById(R.id.BB);
clearbutton = findViewById(R.id.C);
dividebutton = findViewById(R.id.Bdivide);
multiplybutton = findViewById(R.id.Bmulti);
button7 = findViewById(R.id.B7);
button8 = findViewById(R.id.B8);
button9 = findViewById(R.id.B9);
minusbutton = findViewById(R.id.Bsubtract);
button4 = findViewById(R.id.B4);
button5 = findViewById(R.id.B5);
button6 = findViewById(R.id.B6);
plusbutton = findViewById(R.id.Bplus);
button1 = findViewById(R.id.B1);
button2 = findViewById(R.id.B2);
button3 = findViewById(R.id.B3);
button0 = findViewById(R.id.B0);
pointbutton = findViewById(R.id.MCdot);
equalbutton = findViewById(R.id.Bequal);
cuberootbutton = findViewById(R.id.cuberoot);
squarerootbutton = findViewById(R.id.squareroot);
xcubebutton = findViewById(R.id.powerX3);
squarebutton = findViewById(R.id.powerX2);
modulusbutton = findViewById(R.id.Bmod);
exponentbutton = findViewById(R.id.exponent);
tenexponentbutton = findViewById(R.id.X10X);
piebutton = findViewById(R.id.pi);
oneoverxbutton = findViewById(R.id.onebyx);
xfactorialbutton = findViewById(R.id.xfactorial);
stpower2nd = findViewById(R.id.power2nd);
logbutton = findViewById(R.id.log);
lnbutton = findViewById(R.id.ln);
tanhbutton = findViewById(R.id.arc_tan);
coshbutton = findViewById(R.id.arc_cos);
sinhbutton = findViewById(R.id.arc_sin);
sinbutton = findViewById(R.id.sin);
cosbutton = findViewById(R.id.cos);
tanbutton = findViewById(R.id.tan);
absbutton = findViewById(R.id.abs);
floorbutton = findViewById(R.id.floor);
expowerbutton = findViewById(R.id.expower);

tanhbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText("arc_tan");
mTanH = true;
}
});

coshbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText("arc_cos");
mCosH = true;
}
});

sinhbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText("arc_sin");
mSinH = true;
}
});


this is the program error it showed, I tried searching for few questions in this page but I wasn't actually getting the expected answer

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calculator, PID: 24890
java.lang.NumberFormatException: For input string: "arc_sin1"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.calculator.MainActivity$40.onClick(MainActivity.java:648)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Continue reading...
 
Top