Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 38

Thread: Sending two strings into a constructor:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default 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.

    PokerFile class
    void separateHands(String cards)
    	{
    		//ArrayList<String>playerOne = new ArrayList<String>();
    		//ArrayList<String>playerTwo = new ArrayList<String>();
     
    		String[] playerOne = new String[10];
    		String[] playerTwo = new String[10];
     
    		String[] parts = cards.split(" ");
     
    			for(int i=0;i<5;i++)
    			{	
    				playerOne[i]=parts[i];
    				//playerOne.add(parts[i]);
    			}
     
    			for(int j=5;j<10;j++)
    			{
    				playerTwo[j]=parts[j];
    				//playerTwo.add(parts[j]);
    			}
     
    			new WinningHand(playerOne,playerTwo);
    	  }


    ignore the boolean methods I was just building the structure of the program. The print file is what is outputting this:

    null
    null
    null
    null
    null
    null

    public class WinningHand extends PokerFile
    {
    	//ArrayList<String> p1 = new ArrayList<String>();
    	//ArrayList<String> p2 = new ArrayList<String>();
    	String[] p1 = new String[6];
    	String[] p2 = new String[6];
     
    	WinningHand(String[] p1,String[] p2)
    	{
    		this.p1=p1;
    		this.p2=p2; 	
    	}
    	WinningHand()
    	{
     
    	}
     
    	boolean pair(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean threeOfaKind(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean twoPair(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean flush(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean fullHouse(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean fourOfaKind(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean straightFlush(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	boolean royalFlush(ArrayList<String> hand)
    	{
    		return true;
    	}
     
    	void print()
    	{
    		for(String s: p2)
    		{
    			System.out.println(s);
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		PokerFile poker = new PokerFile();
    		WinningHand hand = new WinningHand();
     
    		poker.readFile();
    		hand.print();
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    I am just printing null.
    What variables are being printed?
    Where are the print statements that print them?
    Where are those variables given values?
    Are they given values before they are printed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    String[] playerTwo = new String[10];
    		String[] playerOne = new String[10];
     
     
    		String[] parts = cards.split(" ");
     
    			for(int i=0;i<5;i++)
    			{	
    				playerOne[i]=parts[i];
    				//playerOne.add(parts[i]);
    			}
     
    			for(int j=5;j<10;j++)
    			{
     
    				playerTwo[j]=parts[j];
    				//playerTwo.add(parts[j]);
    			}
     
    			new WinningHand(playerOne,playerTwo);

    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.

    for(int i=0;i<5;i++)
    				playerOne[i]=parts[i];
    				//playerOne.add(parts[i]);
     
    			for(int j=5;j<10;j++)
    				playerTwo[j]=parts[j];
    				//playerTwo.add(parts[j]);
     
    			new WinningHand(playerOne,playerTwo);

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,294
    My Mood
    Mellow
    Thanks
    193
    Thanked 669 Times in 656 Posts

    Default 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:

    hand.print();

    I cannot access the variables inside of the loop.
    Why not?

    Therefore I am not sending anything to the other class.
    That doesn't make sense, or your reasoning isn't easy to follow.

    So I thought I would be clever and try this but it didn't work

    for(int i=0;i<5;i++)
    Avoid hard coding the for() statement values whenever possible.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    Thank you guys.... Yes I am just going to stay with the ArrayList<String>

    --- Update ---

    I cannot just walk away from this. It is bugging me. Here is what I did and I get no errors but no output other then the "Hello" I print in main just to make sure something is working

    public class WinningHand extends PokerCalculator
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
    		//System.out.println(p1.size());
    	}
    	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);
     
    		p1Size= p1.size();
    		p2Size= p2.size();
     
    		this.p1Hand = player1;
    		this.p2Hand = player2;
    	}

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    Do you have a question or problem now? Please be clear when asking it.

    The class has two constructors. When should one or the other be used?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    Well both....

                    this.p1Hand = player1;
    		this.p2Hand = player2;

    I thought this would copy one string to the other string array. However whenever I check its size it is zero.

    When I use a loop in the constructor I get the output.

    for(String s :p1Hand)
    		{
    			System.out.println(s);
    		}

    Therefore my issue is copying the values I am getting into a global private variable so i can use it in the rest of the program to run my checks.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    my issue is copying the values
    You need to track the data values from where it is collected or set through where you want to save it.
    Just writing code to do something is not enough. That code must be executed.

    Can you answer the question I asked at the end of post#7
    The class has two constructors. When should one or the other be used?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    one constructor takes parameters where as the other does not. If I try to execute the program it will ask for two strings unless I make a default constructor.

    Track the data down? The constructor is working but it will not set values outside of it for whatever reason.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    it will not set values
    What variables are supposed to get values?
    Where does the code do that?
    Is that code executed?
    Add a println() statement to see if the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    Norm I did, I put a loop in the constructor like this:

    public class WinningHand extends PokerCalculator
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
    		//System.out.println(p1.size());
    	}
    	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);
     
    		p1Size= p1.size();
    		p2Size= p2.size();
     
    		this.p1Hand = player1;
    		this.p2Hand = player2;
     
    		for(String s :p1Hand)
    		{
    			System.out.println(s);
    		}
     
    	}

    Which results in the output I expected. However when I try to access the p1Hand variable outside of the constructor I get nothing.
    Last edited by jocdrew21; June 13th, 2014 at 12:23 PM.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    I get nothing.
    What is "nothing"? null or empty String or ???

    p1Hand is private. How are you accessing it?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    What is "nothing"? null or empty String or ???
    empty

    p1Hand is private. How are you accessing it?
    in the constructor and setting its values. Then calling in within the same class.

    public class WinningHand extends PokerCalculator
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
    		System.out.println(p1Size);	
    	}
     
    	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;
    	}

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.

    --- Update ---

    Also posted at: Assigning private variables values from constructor:
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    Where is this happening?
    The constructor

    Which statements are doing what needs to be done?
    The constructor

    Try debugging the code by adding some println statements that print out messages when the code at the important points executes
    I did in the constructor but cannot access the in any other method.

    Java For Complete Beginners - class constructor

    has examples of the same way I am trying to set the private variables.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    Did you try adding some println() statements for debugging to see what the code was doing?
    What printed out?

    The constructor
    There are two constructors. Which one is being executed?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    Both

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    Can you make a small, complete program that compiles, executes and shows the problem?

    Without a complete program to test with, this thread is not going any place.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    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);
    	  }

    package pokerHandCalculator;
     
    import java.util.ArrayList;
     
    public class WinningHand extends PokerCalculator
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
    		System.out.println(p1Size);	
    	}
     
    	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;
    	}
     
    void print()
    	{
    		System.out.println("Printing Hand 1");
    		for(String s :p1Hand)
    		{
    			System.out.println(s);
    		}
     
    		System.out.println("Printing Hand 2");
    		for(String s :p2Hand)
    		{
    			System.out.println(s);
    		}
     
    		System.out.println(p1Hand.length);
    	}
     
    	public static void main(String[] args)
    	{
    		PokerCalculator poker = new PokerCalculator();
    		WinningHand hand = new WinningHand();
     
    		poker.readFile();
    		hand.print();
     
    	}
     
    }

    output:
    Printing Hand 1
    Printing Hand 2
    0

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    What is the 0 at the end of the print out? Add a String in all of the println()s to identify what is printed. For example:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    There should be println() statements in ALL the constructors and in ALL of the methods so that you can see where the code is executing.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    621
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default 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.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Apr 2014
    Posts
    199
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Sending two strings into a constructor:

    Yes I did try this already:

    public static void main(String[] args)
    	{
    		ArrayList<String> p1 = new ArrayList<String>();
    		ArrayList<String> p2 = new ArrayList<String>();
     
    		PokerCalculator poker = new PokerCalculator();
    		WinningHand hand = new WinningHand(p1,p2);
     
    		poker.readFile();
    		hand.print();
     
    	}

    I have used this method in C++ many times with no issue. However since it didn't made an empty default constructor. Which is why i said both are executing because I am getting output from both.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Sending two strings into a constructor:

    How does that work? The ArrayLists are empty?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. sending email
    By pradipthite57 in forum Java Networking
    Replies: 3
    Last Post: April 16th, 2014, 02:53 AM
  2. Sorting lowercase strings before uppercase strings
    By keepStriving in forum Java Theory & Questions
    Replies: 4
    Last Post: March 26th, 2014, 03:33 PM
  3. Sending and Receiving File
    By beer-in-box in forum Java Networking
    Replies: 8
    Last Post: March 31st, 2013, 07:57 PM
  4. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  5. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM