aspose file tools*
The moose likes Applets and the fly likes Need help with an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Need help with an applet" Watch "Need help with an applet" New topic
Author

Need help with an applet

Deron Brown
Greenhorn

Joined: Jul 22, 2014
Posts: 28
"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


[Thumbnail for assignment12.png]

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2850
    
  11

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

Joined: Jul 22, 2014
Posts: 28
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]

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2850
    
  11

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

Joined: Jul 22, 2014
Posts: 28
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

Joined: Oct 01, 2001
Posts: 2850
    
  11

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

Joined: Jul 22, 2014
Posts: 28
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

Joined: Jul 22, 2014
Posts: 28
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

Joined: Jul 22, 2014
Posts: 28
Figured it out
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2850
    
  11

Congratulations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with an applet