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 Getting There Object Inheritance

GAI-EWB Team
GAI-EWB Team
8,934 Points

How do I prevent the return statement from breaking up words onto different lines?

I know that %n can be used for print statements but I can't get the same to work when returning the tweet. Mine comes up as this...

This is a new Treet: Treet: "Want to be famous? Simply tweet about java and u se the hashtag #treet. I'll use your tweet in a new @treehouse course about d ata structures." - @craigsdennis

GAI-EWB Team
GAI-EWB Team
8,934 Points

The spaces indicate where it breaks up the word onto the next line.

Stephen Bone
Stephen Bone
12,359 Points

Hi GAI-EWB Team

Are you able to post your code up so I can test it and try to identify where the issue may be?

Thanks Stephen

1 Answer

So we have 2 classes (in the same package): Treet.java and a Main.java (which in the video lessons is the Example.java).

My Treet.java looks like this:

public class Treet {

    private String mAuthor;
    private String mDescription;

    public String getAuthor() {
        return mAuthor;
    }

    public String getDescription() {
        return mDescription;
    }

    public Treet(String author, String description) {
        mAuthor = author;
        mDescription = description;     
    }

    @Override
    public String toString() {
        return "Treet: " + mDescription + " -@" + mAuthor;

    }
}

And the Main.java looks like this:

public class Main {

    public static void main(String[] args) {

        Treet treet = new Treet(
                "craigsdennis",
                "\"Want to be famous? Simply tweet about java and u"+
                "se the hashtag #treet. I'll use your tweet in a new @treehouse course about d"+
                "ata structures.\"");

        System.out.printf("This is a treet: %n %s", treet); // see how the next line appears in output
        System.out.println("");
        System.out.println("");
        System.out.printf("This is a treet: %n%s", treet); // see how the next line appears in output
    }

}

Then my output is the following:

This is a treet: 
 Treet: "Want to be famous? Simply tweet about java and use the hashtag #treet. I'll use your tweet in a new @treehouse course about data structures." -@craigsdennis

This is a treet: 
Treet: "Want to be famous? Simply tweet about java and use the hashtag #treet. I'll use your tweet in a new @treehouse course about data structures." -@craigsdennis

So if I understand you correctly, did you use %n in the Treet constructor's description string in the Main.java class? I guess it is not necessary as you can see from my code.

Can you post your code, if I misunderstood something?

The issue may be that you actually use a %n where you actually break up the code in the editor for better readability. In this case, I would say this is not necessary and this is probably the reason why you get the spaces in the words as you described in your question. You should omit the %n in those places as I did.