What's wrong with my code ? everytime I click the app crashes - android

I'm working on a simple calculator
I'm still a beginner and don't know much about java or android
however, everytime I click on the button the app crashes!
what's wrong with the code ?
final Button btn = (Button) findViewById(R.id.btn);
final EditText ET = (EditText) findViewById(R.id.ET);
final EditText ET2 = (EditText) findViewById(R.id.ET2);
final EditText ET3 = (EditText) findViewById(R.id.ET3);
final TextView TV = (TextView) findViewById(R.id.textView4);
final TextView TV2 = (TextView) findViewById(R.id.textView5);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String et = ET.getText().toString();
String et2 = ET2.getText().toString();
String et3 = ET3.getText().toString();
for (int i = Integer.parseInt(et2); i < Integer.parseInt(et3); i++) {
try {
str = et.replace("X", "" + i);
}
catch (Exception e) {
str = et.replace("x", "" + i);
}
int rslt = Integer.parseInt(str);
TV.append( " " + i )
TV2.append( " " + rslt )
}
}
}
});
That's all codes already
this code is meant to make a table for a F(X) method like the one on the casio calculator. I may need it for school
like :
F(X) = X^2 + X*4 - 3

One problem I see is that outer try misses corresponding catch block and you do not check whether proper number is entered in Et2 and Et3. Integer.parseInt will throw an exception if provided text is not a proper number.

Related

How to properly transform EditText to Int

I am trying to set an easy app that taken two numbers gives the sum of hose two. I found something about how to do this but as I try to convert string to int the app crashes. Can you help me?
Here's my method:
public void do_sum(View v)
{
EditText t1 = (EditText) findViewById(R.id.number1);
EditText t2 = (EditText) findViewById(R.id.button);
String s1 = t1.getText().toString();
String s2 = t2.getText().toString();
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);
int res = n1+n2;
((EditText) findViewById(R.id.result)).setText(String.valueOf(res));
}
Please can you help me?
There is a small error in your code, you should use R.id.number2 instead of R.id.button when getting the second number from the EditText view:
EditText t1 = (EditText) findViewById(R.id.number1);
EditText t2 = (EditText) findViewById(R.id.number2);
Additionally, to avoid crashing the app, you can add a try-catch block to catch the NumberFormatException that might be thrown if the strings in s1 and s2 cannot be parsed into integers:
try {
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);
int res = n1 + n2;
((EditText) findViewById(R.id.result)).setText(String.valueOf(res));
} catch (NumberFormatException e) {
// Handle the exception here
}

Compute all with one button [duplicate]

This question already has answers here:
java.lang.numberformatexception: invalid double: " "
(6 answers)
Closed 6 years ago.
I’m making an app that can compute all I needed with just 1 click of a button, but its forced closing when I'm pushing the “calculate” button, can anyone help me with what I did wrong?
previous = (EditText) findViewById(R.id.editText);
present = (EditText) findViewById(R.id.editText2);
Button calculate = (Button) findViewById(R.id.calculate);
consumption = (TextView) findViewById(R.id.textView1);
basic = (TextView) findViewById(R.id.textView2);
mmp = (TextView) findViewById(R.id.textView3);
stp = (TextView) findViewById(R.id.textView4);
before = (TextView) findViewById(R.id.textView5);
penalty = (TextView) findViewById(R.id.textView6);
after = (TextView) findViewById(R.id.textView7);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
makeCalculations();
}
});
}
private void makeCalculations() {
double n1 = Double.valueOf(previous.getText().toString());
double n2 = Double.valueOf(present.getText().toString());
double n3 = Double.valueOf(consumption.getText().toString());
double n4 = Double.valueOf(stp.getText().toString());
double n5 = Double.valueOf(mmp.getText().toString());
double n6 = Double.valueOf(penalty.getText().toString());
double n7 = Double.valueOf(before.getText().toString());
double n8 = Double.valueOf(basic.getText().toString());
consumption.setText("Your Consumption is: " + (n1 + n2));
basic.setText("Your Consumption is: " + (n3 * 280.00));
mmp.setText("Meter Maintenance Fee: " + 10.00);
stp.setText("Septage Fee: " + (n3 * 0.068) );
before.setText("On/Before Due Date: " + (n3 + n4 + n5));
penalty.setText("Penalty: " + (n3 / 10.00));
after.setText("After Due Date: " + (n6 + n7));
}
}
There is no validation. You should at least check that the string is not empty, but also maybe use a try catch in case a number is not entered. Then you could set the error text on the editText to explain that an invalid number was entered.
So you could do something like this for each double:
boolean answerOk = true;
double n1;
if (!previous.getText().toString().equals("") {
try {
n1 = Double.valueOf(previous.getText().toString());
} catch(NumberFormatException e) {
answerOk = false;
previous.setError("Invalid number");
}
} else {
answerOk = false;
previous.setError("Field is empty");
}
Only run the code at the bottom after all the double's are ok.
if (answerOk) {
// Do the other stuff here
}

Linking 2 EditText to global variable and button to make a phone call

I'm trying to make an app that makes a call based off two editText calls.
The instances are editText1 and editText2. Once filled out, they click add pin to phone. I am able to get it to go to the phone call but only:
*215*null*null
shows up.
So, my question is what am I doing wrong if I want to store whats typed in edit Text1 to be stored in firstNumber and editText2 to be stored in secondNumber?
Furthermore, how do I get the # character to display?
So, the result of what I want is:
*215*firstNumber*secondNumber#
to be displayed on phone call.
public class Main2Activity extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
firstNumber = (EditText)findViewById(R.id.editText2);
secondNumber = (EditText)findViewById(R.id.editText3);
btnAdd = (Button)findViewById(R.id.button5);
Toast.makeText(Main2Activity.this,
"before the call", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:*215*" + firstNumber + "*" + secondNumber + "#" ));
startActivity(intent);
} catch (Exception e) {
}
}
});
}
}
Now that We have it complete using this method `
public void onClick(View v {
try{
EditText et = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.editText5);
String text= et.getEditableText().toString();
String text2 =et2.getEditableText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*215*" + text + "*" + text2 + "#"));
startActivity(intent);
} catch (Exception e) {
}
}
`What i want done is add the Pawn sign or the hash tag at the end of output. For example
*215*text*text2 followed by the hashtag or pawn sign #
but it totally just tosses it out the window after text2.
so at the moment what i get is *215*text*text2 what i want is *215*text*text2#
Thank you so much. I took a different route and it worked this way. everything else was the same from the question.
thanks again
public void onClick(View v) {
try {
EditText et = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.editText5);
String text= et.getEditableText().toString();
String text2 =et2.getEditableText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*215*" + text + "*" + text2 + "#"));
startActivity(intent);
} catch (Exception e) {
}
}
To get the actual text in EditText, you need to use:
firstNumber.getText().toString();
secondNumber.getText().toString();
EDIT: Use the Uri.encode() method to show the # symbol as well
String phoneNumber = firstNumber.getText().toString() + secondNumber.getText().toString() + "#";
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+ Uri.encode(phoneNumber)));
startActivity(callIntent);

How to calculate the sum of EditText boxes?

Hi all I am having a little trouble handling this question. I have 5 EditText boxes and I ask the user to put a number in them. Then I want to calculate the sum of the EditText boxes and do something with the sum. However I am having trouble in the case if the user don't put a number in all the boxes.
Is it possible to put a default value "0" in the boxes just in case not all the boxes are filled but the sum to be calculated although not all the boxes are filled?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_extra_question);
textBox1 = (EditText) findViewById(R.id.editText1);
textBox2 = (EditText) findViewById(R.id.editText2);
textBox3 = (EditText) findViewById(R.id.editText3);
textBox4 = (EditText) findViewById(R.id.editText4);
textBox5 = (EditText) findViewById(R.id.editText5);
buttON = (Button) findViewById(R.id.button1);
buttON.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
number1 = Integer.parseInt(textBox1.getText().toString());
number2 = Integer.parseInt(textBox2.getText().toString());
number3 = Integer.parseInt(textBox3.getText().toString());
number4 = Integer.parseInt(textBox4.getText().toString());
number5 = Integer.parseInt(textBox5.getText().toString());
sum = number1 + number2 + number3 + number4 + number5;
if(sum > 100)
{
Toast.makeText(getApplicationContext(), "Nice!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Fail!", Toast.LENGTH_LONG).show();
}
}
});
Do this for each number:
number1 = textBox1.getText().toString().trim().isEmpty()?0:Integer.parseInt(textBox1.getText().trim().toString());
Create on function like this
public int getIntFromString(String str) {
if (str != null) {
if (str.equalsIgnoreCase("")) {
return 0;
} else {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
return 0;
}
}
} else {
return 0;
}
}
Then Your code should be
number1 = getIntFromString(textBox1.getText().toString().trim());
number2 = getIntFromString(textBox2.getText().toString().trim());
number3 = getIntFromString(textBox3.getText().toString().trim());
number4 = getIntFromString(textBox4.getText().toString().trim());
number5 = getIntFromString(textBox5.getText().toString().trim());
Yes your idea is correct. For each of the edit texts , just check:
if(textBox1.getText().toString().equals(""))
{
number1 = 0;
}
else
number1 = Integer.parseInt(textBox1.getText().toString());
You have to check Empty String using the below code and put "0" in every missing value.
number1 = textBox1.getText().toString().isEmpty()?0:Integer.parseInt(textBox1.getText().toString());
number2 = textBox2.getText().toString().isEmpty()?0:Integer.parseInt(textBox2.getText().toString());
number3 = textBox3.getText().toString().isEmpty()?0:Integer.parseInt(textBox3.getText().toString());
number4 = textBox4.getText().toString().isEmpty()?0:Integer.parseInt(textBox4.getText().toString());
number5 = textBox5.getText().toString().isEmpty()?0:Integer.parseInt(textBox5.getText().toString());

Quadratic formula calculation in android?

Hello guys I m new to android and want to display the quadratic formula calculation here is the code i am doing in android there is no error showing in this code but still on the emulator its not displaying the result :-(
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.btnSubmit);
final EditText et1 = (EditText) findViewById(R.id.etVA);//whats wrong
final EditText et2 = (EditText) findViewById(R.id.etVC);//whats wrong
final EditText et3 = (EditText) findViewById(R.id.etVC);//whats wrong
final TextView answer = (TextView) findViewById(R.id.txtResult);//whats wrong
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String a1 = et1.getText().toString();//whats wrong
double a = Double.parseDouble(a1);//whats wrong
String b1 = et2.getText().toString();//whats wrong
double b = Double.parseDouble(b1);//whats wrong
String c1 = et3.getText().toString();//whats wrong
double c = Double.parseDouble(c1);//whats wrong
double x1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a);//whats wrong
System.out.println("x1 = "+ x1);
String R = x1+ ""; //whats wrong
answer.setText("Your Answer is " + R); //whats wrong
}
});
}
try like this
String a1 = et1.getText().toString();
double a = Double.valueof(a1);
String b1 = et2.getText().toString();
double b = Double.valueof(b1);
String c1 = et3.getText().toString();
double c = Double.valueof(c1);
double x1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println("x1 = "+ x1);
String R = String.valueof(x1);
answer.setText("Your Answer is " + R);
Your program doesn't have any errors and you must provide the valid inputs. Otherwise you will get the output Your Answer is NAN i.e., Not a Number.
The valid inputs in the sense, the values of a, b and c provided must satisfy the equation ax2 + bx + c = 0. So that the quadratic formula can be used find the roots.
Example Valid Inputs :
a = 1.0
b = 3.0
c = 2.0
Output For the above Inputs :
Your Answer is -1.0
I have found the solution. Code was fine but I was only doing a simple logical mistake
final EditText et1 = (EditText) findViewById(R.id.etVA);
final EditText et2 = (EditText) findViewById(R.id.etVC);//that is the mistake it should be like (R.id.etVB)....
final EditText et3 = (EditText) findViewById(R.id.etVC);
call onclicklistner() through View
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Categories

Resources