简体   繁体   中英

Iterating through ArrayList - not printing anything

So i need to create an ArrayList that will only take candidates who's...

  • 'grade' is less than 85%,
  • if 'grade' is less than 85%, test their 'regulation'
  • if their 'regulation' >= to 0.5, test their 'communication'
  • if their 'communication' equals 'average' or 'excellent'

Then add it to the list

I THOUGHT thats what my code was doing, but its not printing anything. Since its not coming up with any errors, it leads me to believe that its not really iterating through it. Or its iterating through the first one (which doesn't meet the criteria) and stops, so it won't have anything to print. Ive looked up other examples and tried implementing and changing a few things but its still not working. To me it looks like it should work so i cant figure out what to change.

Any suggestions would be seriously appreciated!

import java.util.ArrayList;

public class Candidate extends AddressBook {

  private boolean innovation;
  private double grade;
  private double regulation;
  private String communication;

  public Candidate(String fn, String ln, double grade, String comm,
          boolean innov, double reg) {
      super(fn, ln);

      this.grade = grade;
      this.communication = comm;
      this.innovation = innov;
      this.regulation = reg;
}

  public boolean isInnovative() {
      return innovation;
}

  public double getGrade() {
      return grade;
}

  public double getRegulation() {
      return regulation;
}

  public String getCommunication() {
      return communication;
}

public static ArrayList<Candidate> getEligibleCandidates(Candidate[] cands) {
    ArrayList<Candidate> eligibleCandidates = new ArrayList<Candidate>();

    Candidate person = cands[0];

    for (Candidate i : cands) {
        while (i.getGrade() < 85) {
            if (i.getRegulation() >= 0.5) {
                if (i.getCommunication().equals("average")
                        || i.getCommunication().equals("excellent")) {

                    person = i;
                    eligibleCandidates.add(i);

                }
            }

        }

    }

    return eligibleCandidates;
}

public void setCommunication(String comm) {
    this.communication = comm;
}

public void setGrade(double grade) {
    this.grade = grade;
}

public void setInnovation(boolean innov) {
    this.innovation = innov;
}

public void setRegulation(double reg) {
    this.regulation = reg;
}



public static void main(String[] args) {

    Candidate r1 = new Candidate("Elena", "Brandon", 82.30, "poor", true,
            0.5);
    Candidate r2 = new Candidate("Thomas", "Molson", 85.10, "poor", false,
            1.0);
    Candidate r3 = new Candidate("Hamilton", "Winn", 77.77, "average",
            false, 0.8);
    Candidate r4 = new Candidate("Suzie", "Sarandin", 69.93, "average",
            false, 0.0);
    Candidate r5 = new Candidate("Philip", "Winne", 93.03, "average", true,
            1.0);
    Candidate r6 = new Candidate("Alex", "Trebok", 88.61, "poor", true, 0.7);
    Candidate r7 = new Candidate("Emma", "Pivoto", 55.99, "excellent",
            false, 0.8);
    Candidate r8 = new Candidate("John", "Lenthen", 87.49, "excellent",
            true, 0.9);
    Candidate r9 = new Candidate("James", "Lean", 88.00, "excellent",
            false, 0.5);
    Candidate r10 = new Candidate("Jane", "Ostin", 91.20, "average", true,
            0.6);
    Candidate r11 = new Candidate("Emily", "Car", 66.79, "excellent",
            false, 0.3);
    Candidate r12 = new Candidate("Daniel", "", 76.65, "average", true, 0.2);
    Candidate r13 = new Candidate("Neda", "Bazdar", 55.89, "excellent",
            true, 0.5);
    Candidate r14 = new Candidate("Aaron", "Smith", 90.01, "excellent",
            false, 0.3);
    Candidate r15 = new Candidate("Kate", "Hen", 87.9, "poor", false, 0.8);

    Candidate[] cands = { r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11,
            r12, r13, r14, r15 };

    ArrayList<Candidate> people = Candidate.getEligibleCandidates(cands);

    System.out.println(people);

}

}

It stops because

for (Candidate i : cands) {
    while (i.getGrade() < 85) {
        if (i.getRegulation() >= 0.5) {
            if (i.getCommunication().equals("average")
                    || i.getCommunication().equals("excellent")) {
                person = i;
                eligibleCandidates.add(i);
            }
        }
    }
}

Think about it once it enters while (i.getGrade() < 85) it never changes the value for the condition to break out of the loop. This will be an endless for loop. you need to change that to an if statement

for (Candidate i : cands) {
    if (i.getGrade() < 85) {
        if (i.getRegulation() >= 0.5) {
            if (i.getCommunication().equals("average")
                    || i.getCommunication().equals("excellent")) {
                person = i;
                eligibleCandidates.add(i);
            }
        }
    }
}

Once your code enters while (i.getGrade() < 85) it will never exit. That should be an if check, not a loop. You never change the person you are checking against while inside that loop , so it will continue to be true forever once it is true once.

For example, with "Elena", the loop will execute and check is Elena's grade < 85? . Since it is, it will enter the loop. Then, once the loop finishes processing, it will go back to the top of the loop and check, is Elena's grade STILL less than 85? , which will be true because it never changed in the loop.

You should use an if check, like below.

    for (Candidate currentCandidate : cands) {
        if(currentCandidate.getGrade()<85 && currentCandidate.getRegulation() >= 0.5 && (currentCandidate.getCommunication().equals("average") || currentCandidate.getCommunication().equals("excellent"))){
            eligibleCandidates.add(currentCandidate);
        }
    }

It should be:

if ((i.getGrade() < 85) &&
    (i.getRegulation() >= 0.5) &&
    (i.getCommunication().equals("average") || i.getCommunication().equals("excellent"))) {
        person = i;
        eligibleCandidates.add(i);
    }
  }
}

...and not while ...

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