Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Assigning a certain value to a String array?

string array

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 Error

Error

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 21 February 2015 - 09:07 PM

I told myself I am not going to allow myself to get caught and give up. I know, because lack of practice, I won't be able to do this on my own. So here I am! 

 

What I am trying to do is make a little text blackjack game for fun. I feel as if I am on the correct path, but just in case I am not, here is my code.


public class Game {
	
	public void startGame(){
		
		//cards
		String[] cards = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
		
		//your cards
		int x = cards.length;
		int y = cards.length;
		
		//house cards
		int x2 = cards.length;
		int y2 = cards.length;
		
		//your random selection
		int randSelect = (int)(Math.random() * x);
		int randSelect2 = (int)(Math.random() * y);
		
		//house random selection
		int randSelectH = (int)(Math.random() * x2);
		int randSelectH2 = (int)(Math.random() * y2);
		
		//the cards you're dealt
		String cardsDealt = cards[randSelect];
		String cardsDeatl2 = cards[randSelect];
		
		//the cards the house is dealt
		String cardsDealtH = cards[randSelect];
		String cardsDealtH2 = cards[randSelect];
		
		
	}

}

It's relatively mediocre, but I am not ashamed! :) This function gets called on in the main function, if you were curious by any chance. Anyways...

 

What I am trying to do is assign a value (int perhaps) to the String 'cards' (and yes, I know there are more cards then I have, hence the mediocre part! haha)

 

I gave one temp, one lazy attempt that is, because I could already feel the tired irritation coming on. My attempt was something like this:

 

cards[0] = 2;

 

It says I can't convert a String into an int, and it's understandable, but I was able to do something along the line of this before and can't remember how. I believe it was something like parseInt? But I totally forgot how to do that, but I don't think I was thoroughly developed with parseInt anyways... So I may have done it another way. 

 

Thanks! You don't have to solve it for me either, a simple nudge would be just as awesome! 

 

P.S the way I //named my code... is really terrible... Tired! Ignore! haha


Edited by Error, 21 February 2015 - 09:09 PM.


#2 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts

Posted 21 February 2015 - 09:57 PM

If I understand you correctly, you are trying to assign an int to the string array cards[]...is that correct? If so, maybe this will help: Converting Numbers to Strings.


                                                                                                                                                                            FACEB00K Likes this.


#3 Error

Error

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 22 February 2015 - 09:09 AM

If I understand you correctly, you are trying to assign an int to the string array cards[]...is that correct? If so, maybe this will help: Converting Numbers to Strings.

 

So I would turn it into a float? What does a float exactly do? When would I actually want to use a float? I see it quite often and never know when to use it, quite frankly, probably because I never got that far into my book! :( I know it tells it right there in the doc, but I think I need it in layman's terms.

 

But, yes! I am trying to assign a value to a String or maybe string to int? Want I want is if I get dealt a "2" and a "Jack" the 2's value = 2 and the "Jack" maybe has a value of 10. Then the value gets added together, so 12. Then, if I want another card, "hit" else "stop" then the house has to show their cards.


Edited by Error, 22 February 2015 - 09:15 AM.


#4 SdyessDev

SdyessDev

    CC Newcomer

  • Member
  • PipPip
  • 22 posts

Posted 22 February 2015 - 11:24 AM

You can use parseInt on the string to turn it into an integer. http://www.tutorials...er_parseint.htm

 

You cannot make cards[0] = 2. cards is an array of String objects, not integers. cards[0] can equal "2", and then be used as Integer.parseInt(cards[0]) which would return 2 in integer format.

 

Generally with a game like blackjack, you should use a data container to hold the deck so no duplicates come up during play. Generate the cards and place them in the container, then shuffle the deck and deal the cards.


Edited by SdyessDev, 22 February 2015 - 11:32 AM.


#5 PBJ

PBJ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 176 posts

Posted 22 February 2015 - 03:18 PM

@error

I'd suggest looking into enum for this particular issue. The implementation of an enum seems like a perfect fit for your requirements.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download