1
public static void main(String[] args){
........................
for(int i = 0; i<play.size(); i=i+1){
        System.out.println("the Winner is; " +Winners(play).get(i).name);
    }
}

This is the main method. i didnt write down Everything here, because its unnecesary. basically what i am doing, is that i am Calling Winners method that takes ArrayList<Game> as argument. play is of type ArrayList<Game>. on that i want to get the elements stored inside and get their name. so that the winners name is shown on screen. i for loop it because there can be several winners. depends on how many shares the same score.

private static ArrayList<Game> Winners(ArrayList<Game> gamers){
    ArrayList<Game> winner = new ArrayList<Game>();

    for(int i = 1; i==gamers.size(); i=i+1){

        if(gamers.get(i).getPoints()>gamers.get(i-1).getPoints()){     winner.clear();                              
            winner.add(gamers.get(i));
        }
        else if(gamers.get(i).getPoints()<gamers.get(i-1).getPoints()){ winner.clear();
            winner.add(gamers.get(i-1));
        }

        else if(gamers.get(i).getPoints()==gamers.get(i-1).getPoints()){ winner.clear();
            winner.add(gamers.get(i));
            winner.add(gamers.get(i-1));
        }

    }

    return winner;
}

the Winners method returns an Arraylist<Game>, which is where the gamer or gamers with top score are stored. i loop through each on of the gamers and compare their points to each other. the one who has the most score gets stored in the Winner arraylist.

i clear the winner arraylist all the time some elements goes inside, because i only want the top Points stored there.

My issue is, i dont know if i am doing it correct. because i am getting error on on the System.out.println("the Winner is; " +Winners(play).get(i).name);. it says index 0 size 0 (indexoutofboundsexception)

5
  • Why doesnt my elements get stored inside the winner arraylist? Commented Sep 16, 2016 at 13:23
  • 1
    for(int i = 1; i==gamers.size(); i=i+1){ doesn't do what I think that you think it does :) Commented Sep 16, 2016 at 13:24
  • btw you can use i++ instead of i=i+1 Commented Sep 16, 2016 at 13:25
  • Hint, from docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html : When the termination expression evaluates to false, the loop terminates. Commented Sep 16, 2016 at 13:25
  • 1
    My first guess is that your winners function for some reasons is returning an empty arraylist. Commented Sep 16, 2016 at 13:25

1 Answer 1

1
for(int i = 1; i==gamers.size(); i=i+1)

It means the loop will terminate when i==gamers.size() is false. At the first time when i=1, but gamers.size() is greater than 1. It never enters the loop. And returns empty list. When you trying to get one element from an empty list, you are getting the exception.

But I think you want to check i < gamers.size(). It might be:

for(int i = 1; i < gamers.size(); i++)

Again, you don't need winner.clear() in each if block. Rather you could:

private static ArrayList<Game> Winners(ArrayList<Game> gamers) {
    ArrayList<Game> winner = new ArrayList<Game>();

    for (int i = 1; i < gamers.size(); i++) {
        winner.clear();

        if (gamers.get(i).getPoints() > gamers.get(i - 1).getPoints()) {
            winner.add(gamers.get(i));
        } else if (gamers.get(i).getPoints() < gamers.get(i - 1).getPoints()) {
            winner.add(gamers.get(i - 1));
        } else { // last check when points are equal
            winner.add(gamers.get(i));
            winner.add(gamers.get(i - 1));
        }
    }
    return winner;
}

N.B: There is an issue in the approach of your Winners() method. You are iterating over gamers and adding a winner to the list. But every time you are clearing the list. That means you will get only the winner from last two players. Previous winners will be cleared. I think you need to recheck it.

As you are clearing the list, the size of winner list won't be same as the size of play. As a result, you will also get exception in this code.

for(int i = 0; i < play.size(); i=i+1) {
    System.out.println("the Winner is; " + Winners(play).get(i).name);
}

One fix could be:

ArrayList<Game> winnersList = Winners(play);
for(int i = 0; i < winnersList.size(); i++) {
    System.out.println("the Winner is; " + winnersList.get(i).name);
}
Sign up to request clarification or add additional context in comments.

6 Comments

when i change it to i<= gamers.size(); i get Another indexoutofboundsexception. on the first if statements.
actually it would be i < gamers.size(), cause if gamers.size() is 4, you need 3 check.
now after i changed what you said. i get error outofboundexception on the [tag:System.out.println("the Winner is; " +Winners(play).get(i).name);]
why is that? i dont understand
see my edited N.B. part. I think that's the reason. You need to rethink on clearing the list. What's the purpose of clearing?
|

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.