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.

Results 1 to 13 of 13

Thread: Question about using the toString method.

  1. #1
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Question about using the toString method.

    Hi everyone. I'm writing a program that is using a class called Coin.java, that flips a coin. The program is supposed to find the longest run of Heads in 100 flips of the coin. I'm having trouble returning the final value of how many heads were flipped. In order to return this value I need to use the toString method that is included in the Coin class that was provided from my lab book. What paramaters am I supposed to put in the "System.out.println()" statement in order to return the toString? Below is the Coin class with my program below it. Thanks for your help!

    Coin class
    public class Coin
    {
    	public final int HEADS = 0;
    	public final int TAILS = 1;
    	private int face;
    // ---------------------------------------------
    // Sets up the coin by flipping it initially.
    // ---------------------------------------------
    	public Coin ()
    	{
    		flip();
    	}
    // -----------------------------------------------
    // Flips the coin by randomly choosing a face.
    // -----------------------------------------------
    	public void flip()
    	{
    		face = (int) (Math.random() * 2);
    	}
    // ---------------------------------------------------------
    // Returns true if the current face of the coin is heads.
    // ---------------------------------------------------------
    	public boolean isHeads()
    	{
    		return (face == HEADS);
    	}
    // ----------------------------------------------------
    // Returns the current face of the coin as a string.
    // ----------------------------------------------------
    	public String toString()
    	{
    		String faceName;
    		if (face == HEADS)
    			faceName = "Heads";
    		else
    			faceName = "Tails";
    		return faceName;
    	}
    }
    Runs program
    public class Runs
    {
    	public static void main (String[] args)
    	{
    		final int FLIPS = 100; // number of coin flips
    		int currentRun = 0; // length of the current run of HEADS
    		int maxRun = 0; // length of the maximum run so far
    		int countH;
     
    		Coin coin1= new Coin(); // Create a coin object
     
    		coin1.flip(); // Flip the coin FLIPS times, Since flip methods returns "void", no parameters.
     
    		for (int i = 0; i < FLIPS; i++)
    		{
    			currentRun=i;
    			coin1.flip();// Flip the coin & print the result
    			System.out.println(coin1);
    			System.out.println("Current run is: " + currentRun);// Update the run information
    		}
     
    		// Print the results
    		System.out.println(         );
    	}
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Question about using the toString method.

    The toString() method returns a String, and System.out.println() can take a String as a parameter. In other words, just pass the String you want to display into the System.out.println() method.

  3. #3
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about using the toString method.

    So based on the first code above(the Coin class), would I do something like "System.out.println(faceName);" ? Or how would I get the program to return the value in the toString method in the Coin class?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Question about using the toString method.

    how would I get the program to return the value
    Use the return statement with the String you want to have returned by the method. Look at the toString() method in the Coin class. It returns the String: faceName.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about using the toString method.

    Thanks for chiming in Norm. When I try "System.out.println(faceName);" the compiler still says that "faceName cannot be resolved to a variable".

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Question about using the toString method.

    Where is the variable faceName defined? You can not use a variable whose definition is not in scope at that location.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about using the toString method.

    Quote Originally Posted by Norm View Post
    Where is the variable faceName defined? You can not use a variable whose definition is not in scope at that location.
    public String toString()
    	{
    		String faceName;
    		if (face == HEADS)
    			faceName = "Heads";
    		else
    			faceName = "Tails";
    		return faceName;
    	}
    }

    In this piece of code from the Coin class isn't it defined as "String faceName"? Or is it the private int face?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Question about using the toString method.

    Yes, faceName is defined inside of the toString() method. Do you get an error with the code in post#7?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about using the toString method.

    Nope. No errors in the class itself or the toString method. I only get an error when I try to use System.out.println(faceName); or System.out.println(face); or however else I try to call the toString.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Question about using the toString method.

    Do you understand what the scope of definition for a variable is? You can not use a variable if it is not defined or if the definition for the variable is out of scope (not inside of the enclosing pair of {}s)

    Where is the definition of faceName in relation to where your trying to reference it? It must not be in scope because the compiler is giving an error. If the only definition for faceName is inside of the toString() method, then that is the only place that you can reference faceName.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about using the toString method.

    Quote Originally Posted by Norm View Post
    Do you understand what the scope of definition for a variable is? You can not use a variable if it is not defined or if the definition for the variable is out of scope (not inside of the enclosing pair of {}s)

    Where is the definition of faceName in relation to where your trying to reference it? It must not be in scope because the compiler is giving an error. If the only definition for faceName is inside of the toString() method, then that is the only place that you can reference faceName.
    Than how can I reference the toString?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Question about using the toString method.

    toString() is a method in a lot of classes. For what class are you trying to call its toString() method?
    You call the toString()method like any other method:
    String aStr = refToClass.toString();
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Question about using the toString method.

    toString method is to format whatever you want to print out from a class (object).

    The general form is Object newObject = new Object(Constructor parameters );
    The toString method doesn't need anything to passed in. You call it by calling the Object itself using System.out.print(newObject);

Similar Threads

  1. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  2. Using \n escape sequence in a toString method
    By coolidge in forum Java Theory & Questions
    Replies: 4
    Last Post: September 22nd, 2011, 04:14 PM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM