File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String issue in Java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String issue in Java code" Watch "String issue in Java code" New topic

String issue in Java code

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
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.

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423

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

Joined: Oct 21, 2000
Posts: 4351

wow. you are good Ireneusz. i already gave up when i didnt see right away where cardPicked was declared

Visit my download page
I agree. Here's the link:
subject: String issue in Java code
It's not a secret anymore!