Results 1 to 6 of 6
- 09-14-2012, 06:22 AM #1
Senior Member
- Join Date
- Sep 2012
- Posts
- 108
- Rep Power
- 0
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.
-
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.
- 09-14-2012, 07:06 AM #3
Senior Member
- Join Date
- Sep 2012
- Posts
- 108
- Rep Power
- 0
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 10Last edited by rru96; 09-14-2012 at 07:09 AM.
- 09-14-2012, 09:00 AM #4
Senior Member
- Join Date
- Sep 2012
- Posts
- 108
- Rep Power
- 0
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 "); }
But now in the main code I have this trying to declare it:
Java Code:String yourCard1Suit = newSuit();
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
- 09-14-2012, 09:57 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 09-14-2012, 10:46 AM #6
Senior Member
- Join Date
- Sep 2012
- Posts
- 108
- Rep Power
- 0
Similar Threads
-
Comparing 2D string array with 1D string array
By jumpgirl in forum New To JavaReplies: 1Last Post: 03-31-2012, 09:41 PM -
Dealing with floating-point in double and String and convert it to String
By emad7105 in forum New To JavaReplies: 3Last Post: 02-10-2012, 06:26 PM -
Converting image to Point array
By Aster in forum Java 2DReplies: 1Last Post: 12-10-2011, 02:49 PM -
null point exception in array lists
By c_walker in forum New To JavaReplies: 3Last Post: 10-17-2009, 05:38 AM -
code point of stirng to string
By Tamu in forum Advanced JavaReplies: 4Last Post: 11-29-2008, 03:56 PM
Bookmarks