Reputation: 143
All I would like to know what's wrong with this is why do things get pushed to the right when there is 3 numbers a decimal point and a decimal. Why isn't this being aligned? I've had this problem with other code, even copied someones code that should've displayed decimal points aligning in a column but it always doesn't align. Please ignore everything above "_________________________________" line, I know it's not aligned with that, i just want to know why it's doing what it's doing below that line. (why mis aligning everything by pushing top 3 rows to the right)
Book Answer's Console Result in MY eclipse console
What the actual answer is supposed to look like according to book answer result
Books Answer Code
package Ch5_Methods;
import java.util.Scanner;
public class testch5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("%-15s%-15s| %-15s%-15s\n","Celsius","Fahrenheit","Fahrenheit","Celsius");
System.out.println( String.format("%62s"," ").replace(' ', '-') );
for (int c = 40, f = 120 ; c >=31; c--, f-=10) {
System.out.printf("%-15.1f%-15.1f| %-15.1f%-15.2f\n",(float)c,celsiusToFahrenheit(c),(float)f, fahrenheitToCelsius(f));
}
}
/** Convert from Celsius to Fahrenheit */
public static double celsiusToFahrenheit(double celsius) {
return (9.0 / 5) * celsius + 32;
}
/** Convert from Fahrenheit to Celsius */
public static double fahrenheitToCelsius(double fahrenheit) {
return (5.0 / 9) * (fahrenheit - 32);
}
}
I cannot figure out how to align the code, I've tried formatting different ways with printf, I'm still a beginner and i'm sure there is an easier way to do this, but i'm trying to accomplish this having only covered methods, loops, if/else, formatting and other basic stuff.
Upvotes: 1
Views: 960