Need help formatting output
Hey all here is my code:
Code:
public static void main(String [] args){
System.out.println("Feet \tMeters");
for(int i=1;i<=10;i++){
double foot =i;
System.out.print(" "+foot);
System.out.printf("\t "+"%.2f\n",footToMeter(foot));
}
System.out.println();
System.out.println("Meters \t Feet");
for(int i=20;i<=65;i+=5){
double meter =i;
System.out.print(" "+meter);
System.out.printf("\t "+"%.2f\n",meterToFoot(meter));
}
}
public static double footToMeter(double foot){
double meter = 0.305*foot;
return meter;
}
public static double meterToFoot(double meter){
double foot = meter/.305;
return foot;
}
Here is the output:
Code:
Feet Meters
1.0 0.31
2.0 0.61
3.0 0.92
4.0 1.22
5.0 1.53
6.0 1.83
7.0 2.14
8.0 2.44
9.0 2.75
10.0 3.05
Meters Feet
20.0 65.57
25.0 81.97
30.0 98.36
35.0 114.75
40.0 131.15
45.0 147.54
50.0 163.93
55.0 180.33
60.0 196.72
65.0 213.11
My problem is I want the numbers under Feet to line up so it would look like this (move 10.0 over somehow):
Code:
Feet Meters
1.0 0.31
2.0 0.61
3.0 0.92
4.0 1.22
5.0 1.53
6.0 1.83
7.0 2.14
8.0 2.44
9.0 2.75
10.0 3.05
Also I need it to line up under the second Foot column like this:
Code:
Meters Feet
20.0 65.57
25.0 81.97
30.0 98.36
35.0 114.75
40.0 131.15
45.0 147.54
50.0 163.93
55.0 180.33
60.0 196.72
65.0 213.11
How would I do this? Thanks for any help and sorry for the long post.
Re: Need help formatting output
Easy way is to check if the number is less than 10, prepend a space. If it is less than 100, prepend another space. Etc.
Re: Need help formatting output
Quote:
Originally Posted by
AndrewM16921
Easy way is to check if the number is less than 10, prepend a space. If it is less than 100, prepend another space. Etc.
Okay yeah I see what your saying is there no formatting tool or feature that kicks it over to the left instead of the right built in to java?
Re: Need help formatting output
Put a field width modifier after the % sign. In your example, try 6. But read up about how it works in Formatter (Java Platform SE 7 )
Re: Need help formatting output
Not necessary! It can be done with Formatter class.
Regards,
Jim
Re: Need help formatting output
java - Formatting numbers with same amount of padding - Stack Overflow
Can specify a width
Code:
public static void main(String args[])
{
String format = "%10.2f\n"; // width == 10 and 2 digits after the dot
float[] floats = { 123.45f, 99.0f, 23.2f, 45.0f };
for(int i = 0; i < floats.length; i++)
{
float value = floats[i];
System.out.format(format, value);
}
}
Re: Need help formatting output
Quote:
Originally Posted by
jim829
Okay thanks Jim I ended up using a %4.1f and a %6.2f
Code:
public static void main(String [] args){
System.out.println("Feet \t Meters");
for(int i=1;i<=10;i++){
double foot =i;
System.out.printf("%4.1f",foot);
System.out.printf("\t "+"%6.2f\n",footToMeter(foot));
}
System.out.println();
System.out.println("Meters \t Feet");
for(int i=20;i<=65;i+=5){
double meter =i;
System.out.print(" "+meter);
System.out.printf("\t "+"%6.2f\n",meterToFoot(meter));
}
}
public static double footToMeter(double foot){
double meter = 0.305*foot;
return meter;
}
public static double meterToFoot(double meter){
double foot = meter/.305;
return foot;
Thanks again