user2344868
user2344868

Reputation: 39

How to grab value from array list?

A briefing of the program: This program keeps track of people who have airline membership cards and how many points they collect each week. (week1, 2, 3, 4) The information is stored in the array, which then when needed, can be outputted by pressing the "listButton".

I know how to grab a value from the array and simply output it, but unsure how to do so with a loop. See problem area under "totalPointsButton"

public class AirlineCardsView extends FrameView {

    class airline {
        String lastName, firstName;
        int week1, week2, week3, week4;

        airline (int _week1, int _week2, int _week3, int _week4, String _lastName, String _firstName) {
            week1 = _week1;
            week2 = _week2;
            week3 = _week3;
            week4 = _week4;
            lastName = _lastName;
            firstName = _firstName;
        }
    }

    /** Define the ArrayList */
    ArrayList <airline> members = new ArrayList <airline>();

    public AirlineCardsView(SingleFrameApplication app) {
        //GUI stuff
    }// </editor-fold>

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

        String lastName, firstName;
        int week1, week2, week3, week4;

        week1 = Integer.parseInt(weekOneField.getText());
        week2 = Integer.parseInt(weekTwoField.getText());
        week3 = Integer.parseInt(weekThreeField.getText());
        week4 = Integer.parseInt(weekFourField.getText());
        lastName = lastNameField.getText();
        firstName = firstNameField.getText();

        airline c = new airline(week1, week2, week3, week4, firstName, lastName);
        members.add(c);
    }                                         

    private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        String temp = "";

        for (int x=0; x<=members.size()-1; x++) {
            temp = temp + members.get(x).firstName + " "
                    + members.get(x).lastName + ": "
                    + members.get(x).week1 + " "
                    + members.get(x).week2 + " "
                    + members.get(x).week3 + " "
                    + members.get(x).week4 + "\n";
        }
        memberListTArea.setText(temp);

    }             

Here I'm unsure how to initialize the values week1, week2, week3, week4 (for int totalPoints) with the same values stored in the array for the corresponding member.

    private void totalPointsButtonActionPerformed(java.awt.event.ActionEvent evt) {

        int week1, week2, week3, week4; 
        String lastName, firstName;

       String points = "";

        for (int j = 0; j < members.size()-1; j++) {
 //this line checks the inputted name to see if it matches any stored in array.
                if (members.get(j).lastName.equals(lastNameField.getText())) {
                    int totalPoints = week1 + week2 + week3 + week4; //then adds total points
                    }
                }

        }

Upvotes: 0

Views: 1290

Answers (2)

CronbachAlpha
CronbachAlpha

Reputation: 355

You should add get and set methods to your airline class. So when you store an object of airline in the arraylist you can get their values by using "members.get(j).week1, members.get(j).week2, etc"

For example create your airline class like this.

public class airline {
    String lastName; 
    String firstName;
    int week1;
    int week2;
    int week3;
    int week4;

    public airline (int week1, int week2, int week3, int week4, String lastName, String firstName) {
        week1 = this.week1;
        week2 = this.week2;
        week3 = this.week3;
        week4 = this.week4;
        lastName = this.lastName;
        firstName = this.firstName;
    }

    public int getWeek1(){
        return week1;
    }

    public void setWeek1(int newWeek1){
        week1 = newWeek1;
    }

    public int getWeek2(){
        return week2;
    }

    public void setWeek2(int newWeek2){
        week2 = newWeek2;
    }

    public int getWeek3(){
        return week3;
    }

    public void setWeek3(int newWeek3){
        week3 = newWeek3;
    }

    public int getWeek4(){
        return week4;
    }

    public void setWeek4(int newWeek4){
        week4 = newWeek4;
    }

    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String newFirstName){
        firstName = newFirstName;
    }

    public String getLastName(){
        return lastName;
    }

    public void setLastName(String newLastName){
        lastName = newLastName;
    }

    public String toString(){
        return getWeek1() + " " + getWeek2() + " " + getWeek3() + " " + getWeek4() + getFirstName() + " " + getLastName();
    }
}

Upvotes: 0

Kent
Kent

Reputation: 195059

I hope I understood the question correctly....

  • firstly, this line has problem:

    for (int j = 0; j < members.size()-1; j++) {
    

you should remove the -1 or use <=. otherwise you won't reach the last element.

  • the if (members.get(j).lastName.equals(lastNameField.getText())) may throw NPE. (members.get(j).lastName could be null) also the variables defined in this method (week1-4, and the lastname, firstname) don't make much sense.

  • you declare the int totalPoints in if block, which means, it won't be seen outside the if block. It is not big deal, if all your logic is in if block.

  • you could try:

    Airline al = null;
    int totalPoints;
    for (int j = 0; j < members.size(); j++) {
        al = members.get(j);
        if (al.lastName.equals(lastNameField.getText())) {
            totalPoints = al.week1 + al.week2 + al.week3 + al.week4;
        }
    }
    

even better:

    int totalPoints;
    for (Airline al:members) {
        if (al.lastName.equals(lastNameField.getText())) {
            totalPoints = al.week1 + al.week2 + al.week3 + al.week4;
        }
    }
  • to make the codes look better, you could consider add getter/setters in your Airline class. also add a method, int getTotalPoints() return the sum of 4 weeks. Then you don't have to sum it outside the class.

Upvotes: 1

Related Questions