variable might not have been initialized

Discussion in 'Java' started by moon, Mar 2, 2004.

  1. moon

    moon Guest

    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
     
    moon, Mar 2, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    Manavendra Gupta, Mar 2, 2004
    #2
    1. Advertising

  3. moon

    moon Guest

    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" <> wrote in message news:<4044c49e$0$19242$>...
    > 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" <> wrote in message
    > news:...
    > > 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
     
    moon, Mar 3, 2004
    #3
  4. 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) { }
    }
    *****

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Mar 3, 2004
    #4
  5. On Wed, 03 Mar 2004 04:56:44 GMT, Andrew Thompson wrote:

    > 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..


    Well.. since I mentioned it.
    <http://www.physci.org/codes/sscce.jsp>

    There, that's _far_ more descriptive..
     
    Andrew Thompson, Mar 3, 2004
    #5
  6. moon

    moon Guest

    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());

    }
    }


    }
    }


    Andrew Thompson <> wrote in message news:<40pkhwstgd5u$>...
    > On Wed, 03 Mar 2004 04:56:44 GMT, Andrew Thompson wrote:
    >
    > > 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..

    >
    > Well.. since I mentioned it.
    > <http://www.physci.org/codes/sscce.jsp>
    >
    > There, that's _far_ more descriptive..
     
    moon, Mar 3, 2004
    #6
  7. moon

    moon Guest

    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());
    }
    }


    }
    }


    Andrew Thompson <> wrote in message news:<40pkhwstgd5u$>...
    > On Wed, 03 Mar 2004 04:56:44 GMT, Andrew Thompson wrote:
    >
    > > 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..

    >
    > Well.. since I mentioned it.
    > <http://www.physci.org/codes/sscce.jsp>
    >
    > There, that's _far_ more descriptive..
     
    moon, Mar 3, 2004
    #7
  8. On 3 Mar 2004 06:51:13 -0800, moon wrote:

    > 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.

    <snip>
    > 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.

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Mar 3, 2004
    #8
  9. moon

    moon Guest

    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
     
    moon, Mar 3, 2004
    #9
  10. (moon) wrote in message news:<>...
    > 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
     
    Gregory A. Swarthout, Mar 3, 2004
    #10
  11. moon

    nos Guest

    "moon" <> wrote in message
    news:...
    > 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));
    }
    }
    }
     
    nos, Mar 3, 2004
    #11
  12. moon

    marcus Guest

    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.

    moon wrote:
    > 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
     
    marcus, Mar 4, 2004
    #12
  13. On 3 Mar 2004 15:35:20 -0800, Gregory A. Swarthout wrote:
    > Just an observation, but a normal deck of cards has 13 ranks, not
    > 12.


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

    moon wrote:
    > for(int j = 0; j < 12; j++)


    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Mar 4, 2004
    #13
  14. Gordon Beaton <> wrote in message news:<4046f1ed$>...
    > On 3 Mar 2004 15:35:20 -0800, Gregory A. Swarthout wrote:
    > > Just an observation, but a normal deck of cards has 13 ranks, not
    > > 12.

    >
    > Here's another observation: there are 13 values from 0-12 (inclusive).
    >
    > moon wrote:
    > > for(int j = 0; j < 12; j++)

    >
    > /gordon


    *Sigh*

    A further observation:

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

    Greg
     
    Gregory A. Swarthout, Mar 5, 2004
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig
    Replies:
    4
    Views:
    651
    Raymond DeCampo
    Nov 2, 2003
  2. mandy
    Replies:
    14
    Views:
    10,254
    Mike Schilling
    Mar 27, 2006
  3. Lee Fesperman
    Replies:
    20
    Views:
    5,254
    Dale King
    May 22, 2006
  4. Shawn
    Replies:
    8
    Views:
    1,289
    Thomas Hawtin
    Oct 25, 2006
  5. moon
    Replies:
    1
    Views:
    121
    David Dorward
    Jul 9, 2003
Loading...

Share This Page