• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with an applet

 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"In this assignment you will use an applet to display images of playing cards. The applet should load a deck of 52 playing card images from the "images" folder that you downloaded. The applet should shuffle the deck (use a random number generator) and display the first 10 cards of the shuffled deck. Display the cards in two rows of five cards each."

That is my goal for this assignment. I've got my code compiling and I will post it below and I've got an html page but when I try to open it I get an error


Somebody please help I don't understand why this is happening
assignment12.png
[Thumbnail for assignment12.png]
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably comes from line 63, where you refer to selectedSuit[10] (and also selectedCard[11]). You define both these arrays to have size 10 on line 37 and 38. Since Java arrays count from 0, only the indexes 0 through 9 are legal to use.
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:It probably comes from line 63, where you refer to selectedSuit[10] (and also selectedCard[11]). You define both these arrays to have size 10 on line 37 and 38. Since Java arrays count from 0, only the indexes 0 through 9 are legal to use.


Thanks! that did it but the cards I draw are supposed to be 10 random cards, it draws 1 card randomly and displays that 10 times any suggestions?
assignment12.png
[Thumbnail for assignment12.png]
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's generally a bad idea to compare boolean variables with boolean literals using ==. That is, just use newCard instead of newCard == true, and !newCard instead of newCard == false. The reason this is a bad idea is that you can accidentally use the assignment operator = rather than the comparison operator ==, and create a hard-to-diagnose bug in your code.
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:It's generally a bad idea to compare boolean variables with boolean literals using ==. That is, just use newCard instead of newCard == true, and !newCard instead of newCard == false. The reason this is a bad idea is that you can accidentally use the assignment operator = rather than the comparison operator ==, and create a hard-to-diagnose bug in your code.


Umm I changed newcard == false to !newCard and it did nothing
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you changed newCard = false to !newCard. newCard == false would be equivalent to !newCard, but that's not what you had. Second, it didn't do nothing. It exposed your next bug, which unfortunately has similar symptoms. Look into how you set the newCard variable, and in particular once it's set to true, does it ever get set back to false? Put in System.out.printlns if you're having trouble following the flow.

By the way, since you know how to use loops, why don't you use one in place of lines 65 - 74, and another for 79-88? And what is line 63 for?
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:First, you changed newCard = false to !newCard. newCard == false would be equivalent to !newCard, but that's not what you had. Second, it didn't do nothing. It exposed your next bug, which unfortunately has similar symptoms. Look into how you set the newCard variable, and in particular once it's set to true, does it ever get set back to false? Put in System.out.printlns if you're having trouble following the flow.

By the way, since you know how to use loops, why don't you use one in place of lines 65 - 74, and another for 79-88? And what is line 63 for?


63 was a typo I deleted it
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:First, you changed newCard = false to !newCard. newCard == false would be equivalent to !newCard, but that's not what you had. Second, it didn't do nothing. It exposed your next bug, which unfortunately has similar symptoms. Look into how you set the newCard variable, and in particular once it's set to true, does it ever get set back to false? Put in System.out.printlns if you're having trouble following the flow.

By the way, since you know how to use loops, why don't you use one in place of lines 65 - 74, and another for 79-88? And what is line 63 for?


Well I know that it's something with the loops because it only loops through once I'm just having trouble seeing it
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured it out
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic