Hello all, I am writing a toString() i know how to do the basics off this but what im looking to know is how to put a line break between each set of information to get this

public String toString()
{

return
"ball pos:" + this.ball +
"keeper pos:" + this.keeper + "Player:" + this.player;

}

to look like this

"============= A Goal==============
Ball position: (what ever is returned)
Keeper position : (What ever is returned)
Player : (What ever is returned)

At the moment im just getting them clumped together.

Recommended Answers

All 7 Replies

Add "\n" for a line break. You can inline that with the text if you like. ie +"\nBall position: " Edit: You can also look into the String.format() method if you like.

Sorry and if you want to have two things on one line how do you put a space

Like

Player: name Keeper : name

I tried a simple " " inbetween them but didnt work and at the moment im getting

Player: nameKeeper : name

The simple spaces will work just fine. Perhaps you removed them in one of your revisions?

Got it now cheers

For completeness, you should know that there is also

System.getProperty("line.separator")

which returns the correct separator for your system, since Windows still uses the bizarre two-character EOL.
I don't know if just using \n fails on Windows machines, though, so I don't know if this is actually necessary.

bizarre two-character EOL

Line printers printed backwards and forwards. There needed to be a control character to move to the next line directly below the current print head position and not to the head of the line.

Oh, I know the origin of it. It's just bizarre that they're still using it twenty years after line printers ceased to be standard issue. No big thing, though, unless someone checks in a piece of code and it's all of a sudden full of ^M because they've screwed up their Eclipse settings. Ah, well, that's we have perl, isn't it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.