I am creating a Celsius to Fahrenheit conversion table using the printf function.
In my notes, I see that I should be able to center the output using the ^ flag immediately after the % for printf. However, Netbeans always gives me an error message when I run the program.
I was wondering if anybody can help me center my output. My code is as follows.
I have looked through questions that have already been answered. However, they do not correspond with what I was taught in class thus far and as such - I cannot use it.
Any help would be greatly appreciated.
public class CelsToFahrTable {
/**
* Main argument prints a Celsius to Fahrenheit conversion table
* using integer Celsius degrees from 0 to 40
*/
public static void main(String[] args)
{
// declare variables
double Cels; //control variable (Celsius Temperature)
System.out.printf("%10s\t%10s%n", "Celsius", "Fahrenheit");
//prints headers Celsius and Fahrenheit at the top to signify what the data
//below means. Celsius should take up 10 columns then tab to Fahrenheit
//which again takes up 10 columns
for ( Cels = 0; Cels <= 40; Cels++)
//creates count-controlled loop using for statement
//Cels initialized to 0, continues expression until 40 and increments
//by 1 degree each time
System.out.printf("%10.0f%10.1f%n", Cels, 1.8*Cels+32.0);
//creates table showing Celsius temperature followed by its corresponding
//Fahrenheit temperature. Celsius temp is 10 columns with no decimals.
//Fahrenheit temp is 10 columns with a 1 decimal precision.
//Note: I tried to center each field using "^" but it gave me an error
//every time
}//end main
The "^" would be placed in the final:
System.out.printf ("%^10.0f%^10.1f%n", Cels, 1.8*Cels+32.0"
----However when I type that code I get an error in Netbeans.