The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Sending two strings into a constructor:

I did this using ArrayList<String> and my tests worked. Meaning I was able to read the strings in a different class through my constructor. However I want to use a string array because it will be easier to handle when I finish the program. I will send each players poker hand in and figure out who is the winner instead of putting it all onto a ArrayList and having to iterate through it. However whenever I did my check I am just printing null.

Here is the issue. Took me a second to see it. I cannot access the variables inside of the loop. Therefore I am not sending anything to the other class. So I thought I would be clever and try this but it didn't work.

Re: Sending two strings into a constructor:

Norm's questions are the kind of things you should be asking yourself to understand how to troubleshoot and debug your code yourself, so please give them serious consideration - answer them to yourself, and you may find the answer to your problem.

To put a sharper point on Norm's excellent questions, pick this statement apart:

Re: Sending two strings into a constructor:

Another change for the separateHands() method:
Pass it the two ArrayLists where the hands for the two players are to go, don't use "global" class instance variables. That allows the caller of the separateHands() method to control what happens to the hands for the two players.

Re: Sending two strings into a constructor:

Where is this happening? Which statements are doing what needs to be done?
Try debugging the code by adding some println statements that print out messages when the code at the important points executes.

Re: Sending two strings into a constructor:

Its quite obvious why you dont get your desired output in the example program you have posted.
This is how you construct your instance of WinningHand:

WinningHand hand = new WinningHand();

Here you use the constructor which has no parameters.

This is how the constructor is defined:

WinningHand()
{
System.out.println(p1Size);
}

Notice how "p1Hand" and "p2Hand" are never assigned any values in this constructor.
So it is only natural that if you try to print this WinningHand you will get an empty output. Your variables contain empty string arrays of size 0.

Your second constructor could be used to initialize these variables to meaningful values, but that second constructor is never being used.

Re: Sending two strings into a constructor:

We're working on getting the OP to see that so that he can solve these kinds of problems in the future.
I was hoping the OP would find the problem by using println() statements to show what the code does.