简体   繁体   中英

Getting Values from two Inflatable Edittext arrayLists and Calculating Values

I am currently working on a program where I can take two inflatable edittexts and do calculations with them. So I'm trying to get each value, put them into the designated arraylist and calculate values according to the corresponding array, But I am having trouble as I am getting an indexoutofbounds exception and I have no idea if my code is even working. Could I get some help please?

Here is my code.

public class CalculateActivity extends AppCompatActivity {
Button calculate;
LinearLayout courses;
ArrayList<Course> coursesList;
SemesterDatabase db;
TextView gpa;
int percentage = 0;
int hundredth =0;
float averageGrade = 0.0f;

float[] gradePoints = new float[]{};


ArrayList<Float> gradesList = new ArrayList();
ArrayList<Float> scoreList = new ArrayList();
ArrayList<Float> totalList = new ArrayList();



int id;
TextView semester;

class C03171 implements OnClickListener {
    C03171() {
    }

     public void onClick(View view) {
        float gradePointsCalc = 0.0f;
        float totalCrdtHrs = 0.0f;

//EDIT BY Jyoti JK, Thank you for your help

            for (int x = 0; x < scoreList.size()&& x<totalList.size(); x++)

        {CalculateActivity.this.gradesList.add(scoreList.get(x) / totalList.get(x));
        }



        for (int i = 0; i < CalculateActivity.this.coursesList.size(); i++) {
            int crdtHrs = ((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours();
            totalCrdtHrs += (float) crdtHrs;
            averageGrade += (((Float)CalculateActivity.this.gradesList.get(i).floatValue())*(((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours())/totalCrdtHrs);

        }
        float gpaCalc = averageGrade;
        CalculateActivity.this.gpa.setText(String.format("%.3f", new Object[]{Float.valueOf(gpaCalc)}));
    }
}


class C03194 implements DialogInterface.OnClickListener {
    C03194() {
    }

    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.cancel();
    }
}

class C03205 implements DialogInterface.OnClickListener {
    C03205() {
    }

    public void onClick(DialogInterface dialogInterface, int i) {
        CalculateActivity.this.db.deleteSemester(CalculateActivity.this.id);
        CalculateActivity.this.db.deleteAllCourses(CalculateActivity.this.id);
        CalculateActivity.this.onBackPressed();
    }
}



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_calculate);
    setSupportActionBar((Toolbar) findViewById( R.id.toolbar));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle((CharSequence) "Calculate GPA");
    this.db = new SemesterDatabase(this);
    this.id = getIntent().getIntExtra("semesterId", 1);
    this.coursesList = this.db.getAllCourses(this.id);
    this.semester = (TextView) findViewById(R.id.semester);
    this.semester.setText(getIntent().getStringExtra("semesterName"));
    this.gpa = (TextView) findViewById(R.id.gpa);
    this.calculate = (Button) findViewById(R.id.calculate);
    this.courses = (LinearLayout) findViewById(R.id.courses);
    addCourses();
    this.calculate.setOnClickListener(new C03171());

}

public void addCourses() {
    LayoutInflater inflater = LayoutInflater.from(this);
    for (int i = 0; i < this.coursesList.size(); i++) {
        View v = inflater.inflate(R.layout.item_course, null, false);
        this.courses.addView(v);
        final int position = CalculateActivity.this.scoreList.size();
        final int position2 = CalculateActivity.this.totalList.size();
        CalculateActivity.this.scoreList.add(0.0f);
        final EditText Score = (EditText) v.findViewById(R.id.Score);
        final EditText Total = (EditText) v.findViewById(R.id.Total);

        Score.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }


            public void afterTextChanged(Editable editable) {
                percentage = Integer.parseInt(editable.toString());
                float perc = (float) percentage;
                CalculateActivity.this.scoreList.set(position, perc);

            }
        });

        Total.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void afterTextChanged(Editable editable) {
                hundredth = Integer.parseInt(editable.toString());
                float hund = (float) percentage;
                CalculateActivity.this.scoreList.set(position, hund);

            }
        });




    }
}

Basically I think what my code is doing is taking the edittext value from

 Score.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }


            public void afterTextChanged(Editable editable) {
                percentage = Integer.parseInt(editable.toString());
                float perc = (float) percentage;
                CalculateActivity.this.scoreList.set(position, perc);

            }
        });

        Total.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void afterTextChanged(Editable editable) {
                hundredth = Integer.parseInt(editable.toString());
                float hund = (float) percentage;
                CalculateActivity.this.scoreList.set(position, hund);

            }
        });

And getting the values to

for (int x = 0; x < scoreList.size(); x++)

        {
            ArrayList<Float> finalscore = new ArrayList();
            finalscore.add(scoreList.get(x) / totalList.get(x));
            for (int j = 0; j < finalscore.size(); j++)

            {
                CalculateActivity.this.gradesList.add(finalscore.get(j));
            }
        }

And finally calculating it using

 for (int i = 0; i < CalculateActivity.this.coursesList.size(); i++) {

             averageGrade += ((Float)CalculateActivity.this.gradesList.get(i).floatValue())*(((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours());

        }
        float gpaCalc = averageGrade;
        CalculateActivity.this.gpa.setText(String.format("%.3f", new Object[]{Float.valueOf(gpaCalc)}));

But I have absolutely No idea where it went wrong and I have no idea how to see where it went wrong. I would appreciate detailed comments!

Oh and here's The error Log.

FATAL EXCEPTION: main
                                                                             Process: com.example.joon.chadwickgrades, PID: 19395
                                                                             java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                                 at java.util.ArrayList.get(ArrayList.java:411)
                                                                                 at com.example.joon.chadwickgrades.CalculateActivity$C03171.onClick(CalculateActivity.java:66)
                                                                                 at android.view.View.performClick(View.java:5623)
                                                                                 at android.view.View$PerformClick.run(View.java:22433)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6247)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

As your code,

for (int x = 0; x < scoreList.size(); x++)

    {
        ArrayList<Float> finalscore = new ArrayList();
        finalscore.add(scoreList.get(x) / totalList.get(x));
        for (int j = 0; j < finalscore.size(); j++)

        {
            CalculateActivity.this.gradesList.add(finalscore.get(j));
        }
    }

For Every iteration ,

1.You are creating a finalscore arraylist object.

2.And adding only one element to it.

3.And then inner for loop starts. the size of finalscore is always one. And you are adding it to gradesList.

I think you can change it as,

 for (int x = 0; x < scoreList.size() && x<totalList.size(); x++)

    {

       CalculateActivity.this.gradesList.add(scoreList.get(x) / totalList.get(x));

    }

And change the code like,

for (int i = 0; i < CalculateActivity.this.coursesList.size() && i <CalculateActivity.this.gradesList.size(); i++) {
            int crdtHrs = ((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours();
            totalCrdtHrs += (float) crdtHrs;
            averageGrade += (((Float)CalculateActivity.this.gradesList.get(i).floatValue())*(((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours())/totalCrdtHrs);

        }

Edit:

The point is, Whenever you are getting data from the list, You need to check whether it is null and also it's size. which is a best practice to avoid exceptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM