This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

String issue in Java code  RSS feed

 
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.
 
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.
 
Ranch Hand
Posts: 4702
9
Java Scala
  • 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
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!