2

Here is the code.

public class PokerGame
{
    public static void main(String args[])
    {
        Scanner kb=new Scanner(System.in);
        Scanner nkb=new Scanner(System.in);
        Random r=new Random();
        Double jackpot;
        System.out.println("Enter the number of people playing");
        int players=nkb.nextInt();
        List<PokerPlayer> player;
        PokerPlayer[] Playersstorage=new PokerPlayer[players];
        List<String> Names=new ArrayList<String>();
        List<Double> payin=new ArrayList<Double>();
        List<Integer> deck;
        player=new ArrayList<PokerPlayer>();
        Boolean playersinit=true;
        while(playersinit==true)
        {
            for(int x=0;x<players;x++)
            {
                System.out.println("Enter your name please, and then your pay in amount");
                Names.add(kb.nextLine().toLowerCase());
                payin.add(nkb.nextDouble());
            }
            System.out.print("\f");
            playersinit=false;
        }
        Boolean playing=true;
        while(playing==true)
        {
            deck=new ArrayList<Integer>(52);
            final int decksize=52;
            Boolean deckshuffled=true;
            while(deckshuffled==true)
            {
                while(deck.size()<decksize)
                {
                    Boolean selectRandom=true;
                    Integer currentRandom=0;
                    while(selectRandom==true)
                    {
                        Boolean comparecard=true;
                        currentRandom=r.nextInt(52)+1;
                        while(comparecard==true)
                        {
                            if(deck.contains(currentRandom))
                            {
                                comparecard=false;
                            }
                            else
                            {
                                selectRandom=false;
                                comparecard=false;
                            }
                        }
                    }
                    deck.add(currentRandom);
                    if(deck.size()==decksize)
                    {
                        deckshuffled=false;
                    }
                }
            }
            boolean card_distributor=true;
            while(card_distributor==true)
            {
               boolean playerstarter=true;
               Integer o=deck.size()-1;
               Integer pinit=0;
               Integer ps=1;
               while(playerstarter==true)
               {
                   int[] mycards= new int[2];
                   mycards[0]=deck.get(o);
                   deck.remove(deck.size()-1);
                   int newdecksize=deck.size();
                   Playersstorage[pinit]=new PokerPlayer(Names.get(pinit),payin.get(pinit),mycards);
                   player.add(Playersstorage[pinit]);
                   pinit++;
                   o=deck.size()-1;
                   if(ps==players)
                   {
                       playerstarter=false;
                       playing=false;
                       card_distributor=false;
                   }
                   ps++;
               }
            }
            Boolean playingCards=true;
            List<Integer> dealerHand=new ArrayList<Integer>();
            Integer dealing=deck.size()-1;
            jackpot=0.00;
            while(playingCards==true)
            {
                /*for(int x=0;x<3;x++)
                {
                    dealerHand.add(deck.get(dealing));
                    deck.remove(dealing);
                    dealing=deck.size()-1;
                }*/
                try
                {
                    Thread.sleep(1000/1);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                System.out.println("What is your name?");
                String name=kb.nextLine().toLowerCase();
                boolean betting=true;
                while(betting==true)
                {
                    System.out.println("Enter your bet");
                    Integer bet=nkb.nextInt();
                    int indexofbet=Names.indexOf(name);
                    if(bet>player.get(indexofbet).getMoney())
                    {
                        System.out.println("You can not bet that much without selling your first born childs soul, would you like to do this?");
                        String sellchild=kb.nextLine();
                        if(sellchild.equalsIgnoreCase("yes"))
                        {
                            jackpot+=bet;
                            betting=false;
                        }
                        else
                        {
                            betting=true;
                        }
                    }
                    else
                    {
                        jackpot+=bet;
                        betting=false;
                    }
                }
                System.out.println(jackpot);
                try
                {
                    Thread.sleep(1000/1);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                System.out.println("\f");
                if(Names.contains(name))
                {
                    Integer index=Names.indexOf(name);
                    System.out.println(player.get(index).getCards());
                    Boolean notfinished=true;
                    while(notfinished==true)
                    {
                        try
                        {
                            Thread.sleep(1000/1);
                        }
                        catch(InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                        System.out.println("Are you finished looking at your cards "+name+"?");
                        String done=kb.nextLine();
                        if(done.equalsIgnoreCase("Yes"))
                        {
                            System.out.println("\f");
                            notfinished=false;
                        }
                    }
                }
            }
            List<Integer> gameEnd=new ArrayList<Integer>();
            List<Integer> comparison=new ArrayList<Integer>();
            Boolean checkplayersscore=true;
            while(checkplayersscore==true)
            {
                for(Integer x=0;x<players;x++)
                {
                    if(player.get(x).getMoney()==0.00)
                    {
                        System.out.println(player.get(x).getMoney()+player.get(x).getName());
                        gameEnd.add(1);
                    }
                    else
                    {
                        System.out.println(player.get(x).getMoney()+player.get(x).getName());
                        gameEnd.add(2);
                    }
                    comparison.add(2);
                }
                Boolean continuecheck=true;
                int checkover=1;
                while(continuecheck==true)
                {
                    if(gameEnd.contains(1))
                    {
                        Integer remove=gameEnd.indexOf(1);
                        gameEnd.remove(remove);
                        comparison.remove(remove);
                        Names.remove(remove);
                        payin.remove(remove);
                        player.remove(remove);
                        Playersstorage[remove]=null;
                    }
                    if(gameEnd.containsAll(comparison))
                    {
                        continuecheck=false;
                        checkplayersscore=false;
                        playing=false;
                    }
                    if(checkover==players)
                    {
                        continuecheck=false;
                        checkplayersscore=false;
                    }
                    if(player.size()==0)
                    {
                        continuecheck=false;
                        checkplayersscore=false;
                        playing=false;
                    }
                    checkover++;
                }
            }
        }
    }
}

public class PokerPlayer
{
   private String name;
   private Double money; 
   private Integer[] cards;
   public PokerPlayer(String na,Double n,int[] card)
   {
       name = na;
       money=n+0.00;
       cards=new Integer[card.length];
       cards[0]=card[0];
       cards[1]=null;
   }
   public Integer BJScore(List<Integer> mycards)
   {
       /*unfinished code*/
   }
   public Integer[] getCards()
   {
       return cards;
   }
   public void setCards(int card)
   {
       for(int x=0;x<cards.length;x++)
       if(cards[x]==null)
       cards[1]=card;
   }
   public void setMoney(Double d)
   {
       money-=(d+0.00);
   }
   public Double getMoney()
   {
       return money;
   }
   public String getName()
   {
       return name;
   }
}

I am attempting to create a blackjack game for my AP java class, when i go to obtain and print out the cards that you have been given, which is currently only 1 card stored in the 0 index of the array, and a null in the second index(set in the constructor class), it sends me [Ljava.lang.Integer;@72608760 or some other numeral variation(I am assuming that it is varying due to changing cards each time it is called). Does anyone know the reason it is printing this out instead of the actual values that have been stored in the Integer array?

  • possible duplicate of [Why does println(array) have strange output? ("\[Ljava.lang.String;@3e25a5")](http://stackoverflow.com/questions/8410294/why-does-printlnarray-have-strange-output-ljava-lang-string3e25a5). See also: [Simplest way to print an array in Java](http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java) – Paul Bellora Sep 19 '13 at 20:13
  • 1
    That's a duplicate question, but also see [Simplest way to print an array in Java](http://stackoverflow.com/q/409784/1281433). – Joshua Taylor Sep 19 '13 at 20:14
  • Because that's how an Integer array prints. – Hot Licks Sep 19 '13 at 20:15
  • 3
    The answers below are correct, but in future could you cut your code down to a minimum code that would replicate the problem. For example `[boilerPlate] public static void main(String[] args){int[] ints={1, 2}; System.out.println(ints);}` would replicate this problem – Richard Tingle Sep 19 '13 at 20:15
  • What line is printing this out? – BlackHatSamurai Sep 19 '13 at 20:16

2 Answers2

6

That's because array objects don't have a meaningful toString() method, they just use the default toString() in Object, which prints the class name and a memory address. Better use this for printing arrays:

System.out.println(Arrays.toString(array));

Here's a link to the documentation, and don't forget to import the Arrays class, it's in the package java.util.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

Arrays are objects too in Java, but they don't override Object's toString() method. The output that you see is from Object's toString() method. Quoting from Object's javadocs:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

The getClass().getName() part is responsible for the [Ljava.lang.Integer part, and the hashCode, which for objects is usually based on the memory address, is responsible for the changing numerical variation at the end.

To get the desired output, you can use Arrays.toString() passing in your array.

rgettman
  • 176,041
  • 30
  • 275
  • 357