variable might not have been initialized

M

moon

Hi All,
Im getting theh erorr variable might not have been initialized when
trying to compile below:


public class Card
{
private int rank;
private int suit;


//first private final string array
private final String [] strSuits = {"Clubs", "Hearts", "Diamonds",
"Spades"};
//second array to hold string ranks
private final String [] strRanks = {"Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Queen", "King", "Ace" };


//first static boolean test function
private static boolean isValidRank(int possibleRank)
{
if(possibleRank < 0 || possibleRank > 12)
{
return false;
}
return true;
}

//second boolean test function
private static boolean isValidSuit(int possibleSuit)
{
if(possibleSuit < 0 || possibleSuit > 3)
{
return false;
}
return true;
}

//test to see if incoming values is in a valid suit
public Card(int thisSuit, int thisRank)
{
if(isValidSuit(thisSuit))
{
suit = thisSuit;
}
else
{
suit = 0;
}

//test to see if value is in range
if(isValidRank(thisRank))
{
rank = thisRank;
}
else
{
rank = 0;
}
}
// two public methods
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}

//method to return string value
public String suitToString()
{
String suit = "Suit"+suit;
return suit;
}
//method to return rank string value
public String rankToString()
{
String rank = "Rank"+rank;
return rank;
}

}


any help would be great!Thanks!
moon
 
M

Manavendra Gupta

The culprit are your methods suitToString() and rankToString() :
+ suitToString() defines a local variable called "suit" of type String (to
which you try to add the member "suit" of class Card, but the local variable
hides the member)
+ rankToString() defines a local variable called "rank" of type String (to
which you try to add the member "rank" of class Card, but the local variable
hides the member)

Change the name of the String variables in these two methods and you'll be
happy
Cheers
M
 
M

moon

Hi M,
Thanks so much for your help. Im a real newbie-ofcourse tht was already known:>
anyways, now im having some more trouble with this code:
Im getting cannot resolve symbol variable suit and variable rank..help??
public class Deck
{

//reference to 2-dimensional array of Card objects
private Card [] [] cardDeck;

public Deck()
{
cardDeck = new Card[4][13];

//create a loop within within a loop to create new Card objects
for(int i = 0; i < cardDeck.length; i++)

{
for(int j = 0; j < cardDeck.length; j++)
{

//assign them into the array
cardDeck[j] = new Card(i, j);

}
}




}
public Card getCard(int thisSuit, int thisRank)
{

return cardDeck[suit][rank];
}
}






Manavendra Gupta said:
The culprit are your methods suitToString() and rankToString() :
+ suitToString() defines a local variable called "suit" of type String (to
which you try to add the member "suit" of class Card, but the local variable
hides the member)
+ rankToString() defines a local variable called "rank" of type String (to
which you try to add the member "rank" of class Card, but the local variable
hides the member)

Change the name of the String variables in these two methods and you'll be
happy
Cheers
M

moon said:
Hi All,
Im getting theh erorr variable might not have been initialized when
trying to compile below:


public class Card
{
private int rank;
private int suit;


//first private final string array
private final String [] strSuits = {"Clubs", "Hearts", "Diamonds",
"Spades"};
//second array to hold string ranks
private final String [] strRanks = {"Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Queen", "King", "Ace" };


//first static boolean test function
private static boolean isValidRank(int possibleRank)
{
if(possibleRank < 0 || possibleRank > 12)
{
return false;
} return true;
}

//second boolean test function
private static boolean isValidSuit(int possibleSuit)
{
if(possibleSuit < 0 || possibleSuit > 3)
{
return false;
} return true;
}

//test to see if incoming values is in a valid suit
public Card(int thisSuit, int thisRank)
{
if(isValidSuit(thisSuit))
{
suit = thisSuit;
}
else
{
suit = 0;
}

//test to see if value is in range
if(isValidRank(thisRank))
{
rank = thisRank;
}
else
{
rank = 0;
}
}
// two public methods
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}

//method to return string value
public String suitToString()
{
String suit = "Suit"+suit;
return suit;
}
//method to return rank string value
public String rankToString()
{
String rank = "Rank"+rank;
return rank;
}
}


any help would be great!Thanks!
moon
 
A

Andrew Thompson

On 2 Mar 2004 20:41:20 -0800, moon wrote:

Please do not top-post, moon. It makes
conversations difficult to follow.

You might also get some benefit from this..

I have put a 'corrected' SSCCE below.
However, since I was not clear on what
you were doing, I am not sure if it fixes
the problem (or just gets rid of the
compilation errors)
*****
public class Deck
{

/** reference to 2-dimensional array of Card objects */
private Card [] [] cardDeck;

public Deck()
{
cardDeck = new Card[4][13];

//create a loop within within a loop to create new Card objects
for(int i = 0; i < cardDeck.length; i++)

{
for(int j = 0; j < cardDeck.length; j++)
{
//assign them into the array
cardDeck[j] = new Card(i, j);
}
}
}

public Card getCard(int thisSuit, int thisRank)
{
return cardDeck[thisSuit][thisRank];
}
}

class Card
{
Card(int i, int j) { }
}
*****
 
M

moon

Hi Andrew,
sorry i didnt mention what i was trying to do. i want to display all
the cards in a deck..so for example, it should print out: ace of
clubs, two of clubs, three of clubs, ace of spades, two of spades,
etc...
Here's the last part of my code where it would do the print.

public class DisplayDeck
{
public static void main(String [] args)
{
//create new class Deck object

Deck myDeck = new Deck();

for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 12; j++)
{
//assign them into the array
Card thisCard = myDeck.getCard(i, j);
System.out.println(thisCard.rankToString() + "of" +
thisCard.suitToString());

}
}


}
}
 
M

moon

Hi Andrew,
sorry if i didnt mention what i was trying to do before. i want to
print out all the cards in a deck. for example, it should print ace of
clubs, two of clubs, three of clubs, ace of spades, two of spades,
etc.
Heres the last part of my code where it should do the printing. but
instead of printing the strings, its printing the numbers in the
arrays instead.

public class DisplayDeck
{
public static void main(String [] args)
{
//create new class Deck object
Deck myDeck = new Deck();
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 12; j++)
{
//assign them into the array
Card thisCard = myDeck.getCard(i, j);
System.out.println(thisCard.rankToString() + "of" +
thisCard.suitToString());
}
}


}
}
 
A

Andrew Thompson

Hi Andrew,

You are classic. I notice you
are still top-posting.

Strike 1
sorry if i didnt mention what i was trying to do before.

No skin off my nose.
..i want to
print out all the cards in a deck.
Heres the last part of my code where it should do the printing.

What do you mean 'the last part'?
Did you read the link I have mentioned
to you? What does the 'C' stand for,
'last part' perhaps?

Strike 2

You are getting very close to being
plonked here, moon. Please re read
the thread carefully and see if you
can get your act together.

Do not be afraid to ask questions if
you do not understand what I am saying,
but if you ignore me, I will ingore you.
 
M

moon

Well, thanks for taking the time to patronize. I have been posting
questions in various groups but have not come across anything like
this. I am a newbie but everyone starts out shaky and theres no need
to be so demanding. Sorry for wasting everyone's time here.
moon
 
G

Gregory A. Swarthout

Well, thanks for taking the time to patronize. I have been posting
questions in various groups but have not come across anything like
this. I am a newbie but everyone starts out shaky and theres no need
to be so demanding. Sorry for wasting everyone's time here.
moon

Just an observation, but a normal deck of cards has 13 ranks, not 12.

Greg
 
N

nos

moon said:
Hi Andrew,
sorry i didnt mention what i was trying to do. i want to display all
the cards in a deck..so for example, it should print out: ace of
clubs, two of clubs, three of clubs, ace of spades, two of spades,
etc...
-----------------
change 20 to 52 in the 'for' loop at the bottom
this does what you want
sample output
---
7 of Hearts
2 of Diamonds
9 of Diamonds
Queen of Clubs
King of Diamonds
8 of Hearts
3 of Diamonds
Queen of Diamonds
6 of Diamonds
Jack of Hearts
Jack of Diamonds
10 of Diamonds
3 of Spades
Queen of Hearts
Jack of Clubs
2 of Hearts
5 of Clubs
Queen of Spades
5 of Spades
7 of Clubs
---
-----------------
// program that prints 20 random cards
//
// Here is a solution from the master of collections himself,
// Joshua Bloch.
// Mon Oct 6 00:39:19 CDT 2003

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Deal {
public static void main(String args[]) {
// Make a standard 52-card deck
String[] suit =
new String[] { "Spades", "Hearts", "Diamonds", "Clubs" };
String[] rank =
new String[] {
" Ace",
" 2",
" 3",
" 4",
" 5",
" 6",
" 7",
" 8",
" 9",
" 10",
" Jack",
"Queen",
" King" };
List deck = new ArrayList();
for (int i = 0; i < suit.length; i++) {
for (int j = 0; j < rank.length; j++) {
deck.add(rank[j] + " of " + suit);
}
}
Collections.shuffle(deck);
for (int j = 0; j < 20; j++) {
System.out.println(deck.get(j));
}
}
}
 
M

marcus

Moon,
Andrew treats everyone that way. I have been programming for 26 years
in 6 or 7 languages -- I used to prefer basic, now I prefer Java. I
have been posting to newsgroups for as long as I can remember. I used
to bottom post (when posts were tiny), now I top post. It's all good.
 
G

Gregory A. Swarthout

Gordon Beaton said:
Here's another observation: there are 13 values from 0-12 (inclusive).


/gordon

*Sigh*

A further observation:

j < 12 means that only values 0-11 will be used. Ergo, use j <= 12 or
j < 13.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top