I am writing a program so that it creates a 52 card deck and then shuffles it. However, I am having trouble with getting Java write the "cards" (or Strings), into each element of the ArrayList. I know it isn't doing it because every time I test the program with the isEmpty() method, it keeps saying TRUE. By the time the methods have been invoked the computer should have an ArrayList all set to be used for the simulation.
Can anyone tell me where I went wrong?? My code is below.
In the setRanks() method, you have for (i=0; i< _ranks.size (); i++). what does _ranks.size() return the first time through the loop, why, and how does that Cause your problem?
Joined: Jul 05, 2011
I think I figured one part of the problem. I changed the loop in the setRanks() method from a for to a while. It actually returned the right number this time. However, am still having issues with the setDeck() method...
I am thinking its the same problem. I changed the code a little to see what would happen and I am still getting the same error from the compiler.
I think you are jumping ahead of yourself, making changes based on guesses rather than understanding the problem. The problem wasn't that you used a for loop, nor is it better to use a while loop with a magic-number-constant. The problem was that you were looking at the size of the wrong collection (you were calling size() on the list you just created instead of looking at the size of the array which was the source of the information).
What you need to do is stop coding for a while. Take a pencil and paper and start writing down the steps you need to perform to do what you want. Don't write in Java, write in plain english. Then make each statement smaller, creating more sentences, each more specific as to exactly how the step should be done. Repeat until the code required to solve the issue is obvious.