Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String issue in Java code

 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I am currently writing a simulation for dealing with a deck of 96 cards. 5 cards from the deck are taken initially before the do loop and put face up in a sequence next to the deck (this would be done in real life). What I want the program to do is to pick two cards in the following way per cycle in the do-while loop:
(a) pick two cards randomly from the deck
(b) pick two cards randomly from the sequence (there are conditions to this, as you will see in the code below)
(c) pick one card from the deck and one from the sequence (again, conditions, as you can see in my code below).

However, I am having difficulty in getting one particular method to work. It is my pickOneFromDeck() method, which seems to have some trouble with the cardPicked String variable. The compiler requires it to be initialized, which is fine. But whatever or whenever I DO initialize it, instead of placing a card name in the variable, it just puts the initial value (i.e., "empty space" or NULL if I set to null), in and keeps doing this.

I want it to have a name of the color card picked in that space. What am I doing incorrectly that's causing this runtime error. Here is my code below:



Here is the exception class code if you require it...



I know it is a lot to ask it seems, but this is the only part of the code that I am having trouble with. Other than that, it would work fine.

Thank you so much for your assistance and help everyone.
I look forward to hearing from all of you.

S.T.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In lines 121-124
low = 0, high = 9, high-low+1 = 10
expression (Math.random()*(high-low+1) returns values between 0 and 9.999999
9.99999 casted to (int) gives 9
Since the array cardType has 9 elements (from 0 to 8), sometimes the program aborts with index out of bound exception
trying to get cell 9 from the array.
 
Randall Twede
Ranch Hand
Posts: 4439
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow. you are good Ireneusz. i already gave up when i didnt see right away where cardPicked was declared
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic