0

The following code allows the user to enter a football team and a score. If the football team the user entered matches a team already in the arraylist, then it executes the code.

Why does the else statement still execute when the outer if-statement is true? (i.e. the itCounter still increments even though the if-statement is executed). How do I ensure that it is no longer executed after the 'If' is executed?

int homeScore = 0;
int awayScore = 0;

int itCounter = 0;
boolean again = true;
while (again) {
    System.out.println("");
    System.out.println("Enter name of Home team: ");
    final String homeName = input.next();
    Iterator<FootballClub> it = premierLeague.iterator();
    while (it.hasNext()) {
        if ((it.next().getClubName()).equals(homeName)) {
            System.out.println("Enter number of goals scored by " + homeName + ":");
            Scanner input2 = new Scanner(System.in);
            homeScore = input2.nextInt();
            premierLeague.get(itCounter).setGoalsScored(homeScore);
            System.out.println("itCounter value = " + itCounter);
            again = false;
        } else {
            itCounter++;
            System.out.println("itCounter increased, value = " + itCounter);
        }
        if (itCounter == premierLeague.size()) {

            System.out.println("You must enter a valid team!");
            itCounter = 0;
            System.out.println("itCounter increased, value = " + itCounter);
        }
    }
}
6
  • Which if statement are you referring to? Commented Nov 14, 2015 at 19:18
  • You must insert a break statement where you set again = false. Commented Nov 14, 2015 at 19:20
  • 2
    @sstan There is only one if-else statement! Commented Nov 14, 2015 at 19:20
  • 1
    @SamG: I can see that, but I just wanted to make sure you weren't referring to the 2nd standalone if, because what you are asking doesn't make sense. You may think that both the if and else parts are executing, but they are not. Maybe you are just observing 2 different iterations of the while loop. Commented Nov 14, 2015 at 19:22
  • The while loop as-is is the same as for (FootballClub club : premierLeague) { }, and it will iterate all the clubs, entering the else clause for every club that is not homeName. That's as-coded, whether that's as-intended is up to you to decide. Commented Nov 14, 2015 at 19:25

1 Answer 1

5

Break out of the lookup-loop for the team:

boolean found = false;
Iterator<FootballClub> it = premierLeague.iterator();
int itCounter = 0;
while (it.hasNext()) {
    if ((it.next().getClubName()).equals(homeName)) {
        System.out.println("Enter number of goals scored by " + homeName + ":");
        // Scanner input = new Scanner(System.in);
        homeScore = input.nextInt();
        premierLeague.get(itCounter++).setGoalsScored(homeScore);
        again = false;
        found = true;
        break;
    }
}
if( ! found ){
    System.out.println("You must enter a valid team!");
}

Note 1: Take care to restart the loop searching for the team with all variables properly reset.

Note 2: Don't create another Scanner on the same input stream.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.