This should assign variables to my private variables.
However it is not, why?
If I do this:
public static void main(String[] args) { ArrayList<String> p1 = new ArrayList<String>(); ArrayList<String> p2 = new ArrayList<String>(); p1.add("Mike"); p1.add("Drew"); PokerCalculator poker = new PokerCalculator(); WinningHand hand = new WinningHand(p1,p2); poker.readFile(); hand.print(); }
It works and I get string values for player one.
So how can I get the String array from this method:
void separateHands(String cards) { ArrayList<String>playerOne = new ArrayList<String>(); ArrayList<String>playerTwo = new ArrayList<String>(); String[] parts = cards.split(" "); for(int i=0;i<5;i++) { playerOne.add(parts[i]); } for(int j=5;j<10;j++) { playerTwo.add(parts[j]); } new WinningHand(playerOne,playerTwo); }
To assign these private variables:
private int p1Size; private int p2Size; private String[] p1Hand = new String[p1Size]; private String[] p2Hand = new String[p2Size]; WinningHand() { } WinningHand(ArrayList<String> p1, ArrayList<String> p2) { String[] player1 = new String[p1.size()]; String[] player2 = new String[p2.size()]; player1 = p1.toArray(player1); player2 = p2.toArray(player2); this.p1Size= p1.size(); this.p2Size= p2.size(); this.p1Hand = player1; this.p2Hand = player2; }
I even tried a set method but that didn't work either.
WinningHand(ArrayList<String> p1, ArrayList<String> p2) { String[] player1 = new String[p1.size()]; String[] player2 = new String[p2.size()]; player1 = p1.toArray(player1); player2 = p2.toArray(player2); this.p1Size= p1.size(); this.p2Size= p2.size(); this.p1Hand = player1; this.p2Hand = player2; setHands(p1Hand,p2Hand); } void setHands(String[] p1Hand,String[] p2Hand) { this.p1Hand = p1Hand; this.p2Hand = p2Hand; }
I put a loop inside the setHand method that I called in the constructor and I got the desired output. But I want to assign those variables to the private variable so I can use those variable in the rest of the methods.
I even used a getter method and it still didn't work when I called it in main:
I even thought what if I set the values in the default constructor like this:
NOPE DIDN"T WORK EITHER!!!!!!WinningHand() { p1Hand=getHand1(); p2Hand=getHand2(); }
So in closing how can I assign private variables in the WinningHand class by calling WinningHand's constructor in a Method from a different class. Like the above examples try to do without having to place variables into the constructor in main?