Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Getting There Class Review

Saad Zafar
Saad Zafar
905 Points

Creating a String that contains the value of other Strings...

When I took the time to code Treet.java myself based on the requirements I had no problems understanding requirements and the code I wrote was almost exactly what Craig did save for the way I created the "Getter" for the member variables that allow us to see what was passed to the constructor.

I chose to instead of creating three separate "Getters" for each variable...to create a String. Now I know that it might not be a good idea to do it this way, and if someone can please tell me why that is the case...

The code I had was this:

public String getInfo(){ String dateConvert = String.valueOf(mDate); String[] information = new String[] {mAuthor, mDescription, dateConvert}; return information; }

This doesn't compile and states that:

error: incompatible types: String[] cannot be converted to String

When I did a search online the same error code was posted by people setting a String[] to a String object which I don't think I am doing since I am creating a new one...

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

What you are trying to do is actually to implement toString() method, that Craig will be talking in the next video...

So there you can take a look how to write a String representation of Treet...

Concerning your getInfo.

The problem is that your class returns String, but information that you pass in return statement is of type String[]. That is what causes the problem...

public String getInfo(){ // have to return "String"
   String dateConvert = String.valueOf(mDate); 
   String[] information = new String[] {mAuthor, mDescription, dateConvert}; 
   return information;  // information is String[]
}

Again this is not a conventional idea to put everything in array... The conventional way is to write big String as a sum of all others. Please check the next video for more...