• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
John McAlpine
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. and welcome to the Ranch 
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh oh - big problem here:

Do you see the error?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!