Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: array toString

  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array toString

    I am getting an error (toString cannot override Java toString) when compiling this. its probably something really dumb since i am just starting on java. can someone help plz

    Code:
    public class Temperature
    
    {
    	/* Instance variables*/
    	private int [] weekTemps = new int [7];
    	private int i,z;
    	
    	/* constructor */
    	
    	public Temperature(int [] startWeekTemps)
    	{
    		for(i=0;i<7;i++)
    		{
    			weekTemps[i]=startWeekTemps[i];
    		}	
    	}
    
      /* accessor */
      
      public int[] getWeekTemps()
      {
      	return weekTemps;
      }
      
      /* mutator */
      
      public void setWeekTemps(int [] newWeekTemps)
      {
       for(i=0;i<z;i++)
    	{
    	 weekTemps[i]=newWeekTemps[i];
    	}
      }
    
     /* equals method */
     
     public boolean equals(Object Temperature)
     {
      for(i=0;i<7;i++)
      {
      	if (weekTemps[i] == weekTemps[i])
    	{return true;}
    		else
    		{return false;}
      }
     }
     
     /* tostring method */
     public int[] toString()
     {
    	return(weekTemps[])
     }
     
     
     
    
    }

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Location
    127.0.0.1
    Posts
    348
    Thanks
    26
    Thanked 40 Times in 39 Posts
    i'm not a Java wiz, but I guess your code's trying to override the method toString() in java,
    try renaming your function to some other name.. like toStringfunction()

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Please post the compile errors you are getting in the future.

    The above poster is right and wrong (I suppose a consequence of not being a java guy :P )

    Code:
    /* tostring method */
     public int[] toString()
     {
    	return(weekTemps[])
     }
    You basically can't do this. (You don't even have a ; at the end of the statement either).

    The toString method always returns a String first off. With that, when you write a toString method what you want to do is go through each array element, concatenate that element to an already created string (initially empty), and then return that String.

    Also

    Code:
     public boolean equals(Object Temperature)
     {
      for(i=0;i<7;i++)
      {
      	if (weekTemps[i] == weekTemps[i])
    	{return true;}
    		else
    		{return false;}
      }
     }
    That isn't doing what you probably think it's doing. According to your code, it will always return true.

    Why do I know this? Because you're comparing the same thing to itself! So of course it's true. What you want to compare is the weekTemps with the Object that is passed in (presumably the same object type that weekTemps is apart of).
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •