Results 1 to 4 of 4
Thread: Something I am overlooking?
- 04-23-2010, 09:39 AM #1
Member
- Join Date
- Apr 2010
- Location
- Phoenix, AZ
- Posts
- 25
- Rep Power
- 0
Something I am overlooking?
I, like everybody else in the "New To Java" part of the forums am pretty new to Java and have learned the basics and have been piecing other things together as I have gone along. I am trying to create a 5 card poker game and I have gotten the deck creating, and was also able to deal out 5 card hand without any duplicate cards being dealt, but now I am trying to transfer the 5 card hand I created in class Hand, to the Player1 in class Poker. I am trying to keep it simple by just trying to get 1 card to transfer and cannot get that.
No errors show up but the console always displays "[Ljava.lang.String;@42e816".
The Card class I know is working fine, the problem came about when I tried creating Player1 and calling his hand from the Hand class.
Java Code:import static java.lang.System.out; import java.util.*; public class Poker { public static void main(String args[]) { String[] Player1 = new String[5]; String[] Plyaer2 = new String[5]; Hand players = new Hand(); Player1[1] = players.hand1[1]; out.println(Player1); } }Java Code:public class Card { String[] number = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] suit = {"D", "H", "C", "S"}; String[] deck = new String[52]; int j = 0; { //Creating the deck int j is the counter for(int s = 0; s < 4; s++) { int n = 0; for(; n < 13; n++) { deck[j] = number[n] + suit[s]; j = j + 1; } } } }Java Code:public class Hand { String[] hand1 = new String[5]; //Hand for player 1 String[] hand2 = new String[5]; //Hand for player 2 Card hand = new Card(); //Calling the card class Random myRandom = new Random(); //Setting up the random util { for(int i = 0; i < 5; ++i) //This generates the two hands each from String[0] to String[4] making a 5 card hand { hand1[i] = hand.deck[myRandom.nextInt(52)]; hand2[i] = hand.deck[myRandom.nextInt(52)]; } for(int a = 0; a < 5; a++) //After hands are created, this for loop checks for duplicate cards. { int b = 0; for(; b < 5; b++) { if(hand1[a].equals(hand2[b])) //Checks for dupes in each hand { hand1[a] = hand.deck[myRandom.nextInt(52)]; hand2[b] = hand.deck[myRandom.nextInt(52)]; a = 0; //Resets the loop to where it first started looking for duplicates } if (a != b) //This will compare each hand to itself { if(hand1[a].equals(hand1[b])) //Checks for dupes in hand1 { hand1[a] = hand.deck[myRandom.nextInt(52)]; hand1[b] = hand.deck[myRandom.nextInt(52)]; a = 0; //Resets the loop to where it first started looking for duplicates } if(hand2[a].equals(hand2[b])) //Checks for dupes in hand2 { hand2[a] = hand.deck[myRandom.nextInt(52)]; hand2[b] = hand.deck[myRandom.nextInt(52)]; a = 0; //Resets the loop to where it first started looking for duplicates } } } } } }
- 04-23-2010, 09:52 AM #2
Hi,
you have to post the code with codeta.your code is not clear.
Another thing is you are trying to print the array object ...thats why its displaying like this...
just iterate and print the contents of array.Ramya:cool:
- 04-23-2010, 11:47 AM #3
Member
- Join Date
- Apr 2010
- Location
- Phoenix, AZ
- Posts
- 25
- Rep Power
- 0
Oh, I see, yep, I knew I overlooked something simple, thanks for your help. Was working on the code for a while and I guess I just got kinda burnt out. Still am a noob after all. :)
- 04-23-2010, 12:33 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
out.println(Player1);
Player1 (note that variables in Java should generally start with a lower case) is a String[], which does not have a toString() method associated with it so it uses the default toString in this case, which prints out things in the way you just saw...class name and hashcode.
You'll want to loop round the array printing out each string if you want to see them. I expect there's a utility somewhere for printing arrays, but it's not something I tend to have to do.


LinkBack URL
About LinkBacks

Bookmarks