This site requires JavaScript, please enable it in your browser!
Greenfoot back
Hiro
Hiro wrote ...

2015/2/25

Iterator?

Hiro Hiro

2015/2/25

#
Hello! I would like to ask once again for help. I'm programming a Deal or No Deal game in Java as an assignment. Everything works perfectly well except for this part:
 public void jumble()
  {
    int y = 0;
    ArrayList<Double> prizesOrdered = new ArrayList();
    for (double x : this.originalPrize) {
      prizesOrdered.add(Double.valueOf(x));
    }
    Collections.shuffle(prizesOrdered);
    for (Iterator iterator = prizesOrdered.iterator(); iterator.hasNext();)
    {
      double x = ((Double)iterator.next()).doubleValue();
      this.prizes[y] = x;
      y++;
    }
  }
I noticed that whenever I run the game, there is a particular briefcase that doesn't contain any value in it which results to the failure of the whole program. The briefcase without a value changes everytime I reset the game. I hope that someone can enlighten me what's the problem with the code I posted above. Thank you very much!
davmac davmac

2015/2/25

#
I do not see anything wrong with the code that you've posted. How do you know that it's this part of code that isn't working?
danpost danpost

2015/2/25

#
Hiro wrote...
I noticed that whenever I run the game, there is a particular briefcase that doesn't contain any value in it which results to the failure of the whole program.
"noticed" -- how? "doesn't contain any value" -- do you mean 'null'? "failure" -- was an exception thrown -- and if so, what did it say?
The briefcase without a value changes everytime I reset the game.
Is this something that is found out before or after the 'shuffle' method is executed? If you are getting a NullPointerException error, posting its output and indicating which line the error occurred on could help. Out of curiosity, exactly what is 'originalPrize' (how is the field declared/initialized/assigned values)?
You need to login to post a reply.