programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Trying to figure out if my method is in an endless loop

John McAlpine
Greenhorn
Posts: 2
I have made a simple program to combine two arrays into one array randomly,  resulting in a list of unique playing cards. When I run my program, the console doesn't print anything so I'm assuming the deck creation function is stuck in some kind of infinite loop but I can't figure out why,

Ron McLeod
Bartender
Posts: 1603
232
You have some magic numbers in your code and it is not obvious if they are correct or not.

Try defining constants based on the number of card values and suits such as:and replace the magic numbers with the constants.

Ron McLeod
Bartender
Posts: 1603
232
.. and welcome to the Ranch

Piet Souris
Master Rancher
Posts: 2044
75
Have a look at line 16. What are the possible values for randomSuit? Can you fill 52 unique cards with these values? (and have a look at line 15 as well).

A non random way of filling your deck is: if you have the sets {a, b, c} and { 1, 2, 3, 4}, then form the set (a1, a2, a3, a4, b1, et cetera}.
But that leaves the problem of gettig your deck in a random order....

John McAlpine
Greenhorn
Posts: 2
Thank-you guys so much; I was under the impression that I was getting the random integers (0, 1, 2, 3) with my nextInt(3) function, and I'll work on my magic number usage as well.

Campbell Ritchie
Marshal
Posts: 56570
172
Welcome to the Ranch, again

What does the printList() method in line 37 do? And what is it supposed to do? I am pretty sure the two are different.

There is a basic form for a for loop:-Start from that format when you write for loops. You may have to write something different, but do that by changing the basic form rather than writing something new. There is a reason for the basic form: it works  As you have been told, use a variable rather than a magic number in the loop. Another thing about that basic form: you can usually tell how many times the loop will run by reading the header. Now look at line 38 and work out how many times will that loop will run.

Fred Kleinschmidt
Bartender
Posts: 571
9
Uh oh - big problem here:

Do you see the error?

 It is sorta covered in the JavaRanch Style Guide.