Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

Im brand new to Android and Java coding. Ive been trying to figure how to

Take 4 EditText boxes and calculate them using an array or ArrayList

Then display the Calculation to another EditText Box.

I used double to store the Numbers as they will have a decimal.

Here is the layout - 2017-10-17_1457[^]

What I have tried:

I use

double grade1;
double grade2;
double grade3;
double grade4;
double totalGrade;


To store it in the Global area

I googled and even checked youtube and i couldnt find anything that helped me. I mean there are some examples but nothing i understood.
Posted
Updated 18-Oct-17 7:19am

What you are trying to do is so, so trivial, I don't know how you could've searched any length of time at all and not have found dozens, if not hundreds, of examples. Anyway...

In the onClick() handler for the "Calculate Grade" button, call the getText() method for each EditText widget to get its value, and then call Double.parseDouble() to convert that value and assign it to each of your grade# variables. For example:
EditText et = (EditText) findViewById(R.id.grade1);
grade1 = Double.parseDouble(et.getText().toString());
Since it is read only, you should consider change the "Percentage" EditText to be a TextView widget instead. Then you can assign the sum to it like:
TextView tv = (TextView) findViewById(R.id.pct);
tv.setText(String.valueOf(grade1 + ...));
 
Share this answer
 
So you can put all your grade variables to a arrayList and iterate the array to get the sum.
double totalGrade=0;
ArrayList<Double> arr =new ArrayList();//create a arraylist
//adding values to the arraylist
arr.add(grade1);
arr.add(grade2);
arr.add(grade3);
arr.add(grade4);

//Iterating  through the array list to get sum
for(Double d:arr){
   totalGrade+=d;
}
System.out.println("Total : "+double totalGrade);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900