Results 1 to 6 of 6
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Have string array point to int[] value

    I am trying to slowly build on some examples and the first I am doing is a 2 card blackjack, just to get used to writing.
    Basically I have a y1 and y2 String variable (Math.random()*13) which I have pointing to a String[]. Each array value is a string which contains the numerical AND text spelling of the value. String[0] = "11 - Ace"

    Now, I want the person to see the String value the way I typed it, but then want to apple String[0] to int[0] = 11. Now, of course I keep getting errors which I completely understand. Cannot convert from string to int. So guess the question is how would I go about converting the String[] position to the int[] position value to pass on? Still trying to wrap my head around the basics so sorry if I am beyond confusing.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Have string array point to int[] value

    You're far better creating a class that holds the Card's value and suit rather than trying to convert to and from a String.

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Have string array point to int[] value

    I guess I am still confused on how to link the String value to the int value in the same class.
    I was wanting to see the card "ACE" but then add the number value behind the scene of course then compare mine to the house. Simple blackjack. No suits. Havent learned how to ask for a hit yet. :) Sometimes it is hard for me to see the value, and since JACK, QUEEN, KING are String, this is where the problem started. String KING == int 10
    Last edited by rru96; 09-14-2012 at 07:09 AM.

  4. #4
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Have string array point to int[] value

    So, I sat back and thought about everything. And I tried. And this is what I did.

    Java Code:
     
      public static String newSuit()
      {
        
        int newSuit = (int)(((Math.random()*4)));
        
        if(newSuit == 0)
        {
        return (" of Spades "); 
        }
        else if(newSuit == 1)
        {
        return (" of Hearts ");
        }
        else if(newSuit == 2)
        {
        return (" of Clubs ");
        }
        else if(newSuit == 3)
        {
        return (" of Diamonds ");
        }
    At first I was getting an error saying No Return...
    But now in the main code I have this trying to declare it:
    Java Code:
       String yourCard1Suit = newSuit();
    I am getting an error of missing return statement. This seems to be an idiotic way of picking a random suit. What am I missing that might be causing the missing return statement error?

    Adding on an extra return that will never be reached works, though seems wrong still. I put:
    [CODE]
    return(""); on the end.

    I also created a newCard() which simply picks a random from 1-13. But the problem is there is no such thing as a 12 of Clubs, or a 13 of Diamonds.
    Last edited by rru96; 09-14-2012 at 09:12 AM. Reason: updated

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Have string array point to int[] value

    Which is why you want to model this as classes.
    Think about what properties a card has and then create a class that represents that.
    Not only will this get you used to the idea of classes (which are fundamental to Java) but it also opens up enums.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Have string array point to int[] value

    Thanks for the help. I'm gonna keep trying until something makes sense.

Similar Threads

  1. Comparing 2D string array with 1D string array
    By jumpgirl in forum New To Java
    Replies: 1
    Last Post: 03-31-2012, 09:41 PM
  2. Replies: 3
    Last Post: 02-10-2012, 06:26 PM
  3. Converting image to Point array
    By Aster in forum Java 2D
    Replies: 1
    Last Post: 12-10-2011, 02:49 PM
  4. null point exception in array lists
    By c_walker in forum New To Java
    Replies: 3
    Last Post: 10-17-2009, 05:38 AM
  5. code point of stirng to string
    By Tamu in forum Advanced Java
    Replies: 4
    Last Post: 11-29-2008, 03:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •