java - How to grab value from array list? -
a briefing of program: program keeps track of people have airline membership cards , how many points collect each week. (week1, 2, 3, 4) information stored in array, when needed, can outputted pressing "listbutton".
i know how grab value array , output it, unsure how 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 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 = ""; (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 initialize values week1, week2, week3, week4 (for int totalpoints) same values stored in array corresponding member.
private void totalpointsbuttonactionperformed(java.awt.event.actionevent evt) { int week1, week2, week3, week4; string lastname, firstname; string points = ""; (int j = 0; j < members.size()-1; j++) { //this line checks inputted name see if matches stored in array. if (members.get(j).lastname.equals(lastnamefield.gettext())) { int totalpoints = week1 + week2 + week3 + week4; //then adds total points } } }
i hope understood question correctly....
firstly, line has problem:
for (int j = 0; j < members.size()-1; j++) {
you should remove -1
or use <=
. otherwise won't reach last element.
the
if (members.get(j).lastname.equals(lastnamefield.gettext()))
may throw npe. (members.get(j).lastname
null) variables defined in method (week1-4, , lastname, firstname) don't make sense.you declare
int totalpoints
in if block, means, won't seen outside if block. not big deal, if logic in if block.you try:
airline al = null; int totalpoints; (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; (airline al:members) { if (al.lastname.equals(lastnamefield.gettext())) { totalpoints = al.week1 + al.week2 + al.week3 + al.week4; } }
- to make codes better, consider add getter/setters in
airline
class. add method,int gettotalpoints()
return sum of 4 weeks. don't have sum outside class.
Comments
Post a Comment