Reputation: 333
I've created in my application a few EditText fields and a button. If you put values in the first and in the 2nd EditText fields, when you press the button, the 3rd EditText field shows the sum of the 2 values you entered. That's working fine, but I want to calculate an average. I mean, if I have 4 EditText fields, I want to enter values in 3 (or less) of them and the 4th to show me the average calculating from the sum divided with the number of the values entered. Example: first value: 2, 2nd value: 4 and the average = 2+4=6/2=3. I want to create the case when a EditText field or more is not enter and the application still can calculate the average. Hope you understand what I want. Thanks !
Here is my:
Medii.java:
package com.cngcnasaud.orar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Medii extends Activity {
EditText txtNum1;
EditText txtNum2;
EditText txtTotal;
Button btnCompute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medii);
txtNum1 = (EditText) findViewById(R.id.editText1);
txtNum2 = (EditText) findViewById(R.id.editText2);
txtTotal = (EditText) findViewById(R.id.editText3);
btnCompute = (Button) findViewById(R.id.bmedii);
btnCompute.setOnClickListener(new ClickButton ());
}
private class ClickButton implements Button.OnClickListener{
@Override
public void onClick(View v) {
int x = Integer.parseInt(txtNum1.getText().toString());
int y = Integer.parseInt(txtNum2.getText().toString());
int total = (x + y)/2;
txtTotal.setText(Integer.toString(total));
}
}
}
Upvotes: 0
Views: 12018
Reputation: 99
If I were you I did something different:
A single editText and two buttons: Add number and Calculate.
Every time you hit Add number, capture and put it on an Array.
When you hit Calculate, do a loop to sum the whole numbers in a variable, then do the average as sum_variable/array.lenght.
This way you can sum infinite numbers without creating just a static number of fields.
Good luck!
EDIT: Follow code:
public class MainActivity extends Activity {
public static ArrayList<String> num = new ArrayList<String>();
double sum, res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextNum = (EditText)findViewById(R.id.editTextNum);
Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
Button buttonCalc = (Button)findViewById(R.id.buttonCalc);
final TextView textViewRes = (TextView)findViewById(R.id.textViewRes);
buttonAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String numCatch=editTextNum.getText().toString().trim();
if(numCatch.length() != 0){
num.add(numCatch);
editTextNum.setText("");
}
textViewRes.setText("The average is ");
}
});
buttonCalc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
DecimalFormat format = new DecimalFormat("#.##");
for(int i=0;i<num.size();i++){
sum = Integer.parseInt(num.get(i)) + sum;
}
res = sum/num.size();
textViewRes.setText("The average is "+format.format(res));
res = 0;
sum = 0;
num.clear();
}
});
}
Upvotes: 1
Reputation: 6381
If I understand your question right, you have 3 edittexts and the 4th one should display the average. I haven't tested it so there might be some typos.
String et1 = editText1.getText().toString();
String et2 = editText2.getText().toString();
String et3 = editText3.getText().toString();
int num1 = Integer.parseInt(et1);
int num2 = Integer.parseInt(et2);
int num3 = Integer.parseInt(et3);
int calculate = num1 + num2 + num3/3;
int result = calculate;
editText4.setText(" " + result);
Upvotes: 0
Reputation: 681
Count how many of the fields are not blank, and divide your total by that number. It's probably a good idea to check for blank or not-parsable before you use Integer.parseInt anyways. :)
Upvotes: 1