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
                    }
                }

        }

Answers


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.

Need Your Help

Horizontal overflow occuring with CSS3 3D transformations

3d css3 scrollbar overflow transformation

I have a problem on http://mr.si/ with CSS3 3D transformation. I am doing it plainly for fun and exploring new CSS functions but it disturbs me.

Not able to resize image using ImageMagick Node.js

javascript node.js imagemagick

I am trying to resize an image using ImageMagick and I've referred this example

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.