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.

Extra spaces in output - why & how to fix?

Hi! I'm working on a Java assignment in my beginning Java class. The assignment is to write a program using sqrt() method in the Math class and use a for loop to product the output results. My output is correct according to the chart the instructor gave. My only problem is the extra carriage returns between the output. I don't want any spaces between the output.

Re: Extra spaces in output - why & how to fix?

My output is correct according to the chart the instructor gave. My only problem is the extra carriage returns between the output.

Correct, but not correct? So what is the desired output, and what is the output you observe? It would be a good idea to describe the desired output precisely: ie, indicate in some way whether the horizontal whitespace in the instructor's chart is made up of space characters or a tab character.

-----

printf() format strings are powerful and flexible. Perhaps you could consider altering the width of the numerals you display rather than using the " " and "\t " strings. Also format strings can use %n which produces a newline in a way that looks the same (same vertical space) on any operating system.

Without knowing the output you are supposed to get its impossible to say for sure.

Re: Extra spaces in output - why & how to fix?

You can request it, I guess. But there are other things in the thread that might prove valuable to others: the advice to be precise - really precise - about format before attempting to implement that format in code, and the advice to use the newline format and the width specifiers in preference to tab characters.

The (amended) original post doesn't show the output lining up at all. And it is still not clear whether the output ought to include tab characters.

-----

Here's an example of a table whose format is implemented in code using those suggestions. It shows radius, the square of the radius, and the area of a circle (to 3dp). The intention is for the numerals to be aligned at the units place, be centred (or one character left of centre) in their column, and for there to be two characters between adjacent columns.