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.
This is how the assignment should look:
Code :
Number SquareRoot
0 0.0000
2 1.4142
4 2.0000
6 2.4495
8 2.8284
10 3.1623
12 3.4641
14 3.7417
16 4.0000
18 4.2426
20 4.4721
Which part of my program is causing the extra spaces? How do I fix it?
Code :
public class Week5Assignment {
public static void main(String[] args) {
// Print title of square root chart
System.out.println("Number" + "\tSquare Root");
// Print square root of even numbers 0 - 20
for (int x = 0; x <= 20; x++){
if (x % 2 == 0)
System.out.printf(" " + x + "\t " +
"%.4f", x + Math.sqrt(x));
System.out.println("\n");
}
}
}
This is the part of the output from my code (I didn't post all through #20 since the length is long):
Code :
Number Square Root
0 0.0000
2 3.4142
4 6.0000
6 8.4495
I'm wondering why I'm getting all the spaces between each line. Which part of the code is doing that?
Re: Extra spaces in output - why & how to fix?
Quote:
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?
And please edit your post so the code is displayed properly. Put [code] at the start of the code and [/code] at the end. The idea is to preserve formatting. See the faq.
Re: Extra spaces in output - why & how to fix?
Well, I don't know how to get the formatting correct on my output sample. The '0, 2, 4, etc.' should be centered under 'Number' and '0.0000, etc.' centered under Square Root'.
Re: Extra spaces in output - why & how to fix?
Quote:
Well, I don't know how to get the formatting correct on my output sample.
The code tags should also do that. (It's a bit of an abuse of markup, but that's the way people commonly do it.)
Quote:
The '0, 2, 4, etc.' should be centered under 'Number' and '0.0000, etc.' centered under Square Root'.
Here we have a bit of a problem because "Number" has six characters, so you can't exactly centre "0" beneath "Number". Ditto the 6 character numerals under "Square Root".
Did you understand what I meant by describing whether there really were tab characters in the output you were supposed generate? A string like "Number"+"\tSquare Root" could end up printed like
or
depending on how tab characters are rendered. If the,re is some clear indication that you are supposed to use tab characters, fair enough. But, otherwise, they are better avoided imho.
-----
Sorry to be fussy. But formatting output is a fussy business.
Re: Extra spaces in output - why & how to fix?
Forget it. Can't win with the formatting. -- Marking this as solved.
Re: Extra spaces in output - why & how to fix?
Is there a way to delete this thread? Do I need to request a mod to delete it? I can't get the formatting correct for people to be able to help.
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.
Code :
public class FormatExample {
public static void main(String[] args) {
System.out.println("Radius Square Area (pi r^2)");
System.out.println("------ ------ -------------");
for(int r = 0; r < 30; r += 2) {
System.out.printf(" %2d %4d %8.3f%n", r, r * r, Math.PI * r * r);
}
}
}
The output is:
Code :
Radius Square Area (pi r^2)
------ ------ -------------
0 0 0.000
2 4 12.566
4 16 50.265
6 36 113.097
8 64 201.062
10 100 314.159
12 144 452.389
14 196 615.752
16 256 804.248
18 324 1017.876
20 400 1256.637
22 484 1520.531
24 576 1809.557
26 676 2123.717
28 784 2463.009