The code:
// This program will read in from a file a series of numbers in degrees // Celsius, convert them to degree Fahrenheit and print out a table of // those values. The file input is handled using the command line's // redirection mechanism. import java.util.Scanner; public class Lab4b { public static void main(String[] args) { // Declare variables Scanner scan = new Scanner(System.in); double degreeCelsius; double degreeFahrenheit; // Print to the screen the header of the output table // as seen in the sample run below. // There are two lines in the header. // Make sure it has the right spacing to line up with the table. System.out.println("Celsius | Fahrenheit"); System.out.println("-----------+-----------"); // Write your loop here // We want to loop until there are no more inputs to be read. while( scan.hasNext()) { // Check to see if the input is a double. // If a double read in and store in degreeCelsius. // If not a double print an error message and quit. if (scan.hasNextDouble()) degreeCelsius = scan.nextDouble(); else { System.out.println("Error in input"); System.out.println("Programming is terminating..."); System.exit(0); } // Convert input degrees Celsius to degrees Fahrenheit // and store in degreeFahrenheit. // f = c * (9.0/5.0) + 32.0 degreeFahrenheit = degreeCelsius * (9.0/5.0) + 32.0; // Display to the screen the output as shown in the // sample run below. Use System.out.printf. Make sure // that everything lines up properly. System.out.printf(" degreeCelsius%d| degreeFahrenheit%d%n"); } // End of loop } // End of main method } // End class
I cant figure out what I am doing wrong in the loop.
When I try and compile I get an initialization error
C:\Users\Mitchell\My Documents\cs1113\Labs>javac Lab4b.java
Lab4b.java:52: error: variable degreeCelsius might not have been initialized
degreeFahrenheit = degreeCelsius * (9.0/5.0) + 32.0;
So.. I set degreeCelsius = 0;
Then it compiles but when I run the command java Lab4b < lab4data.txt
C:\Users\Mitchell\My Documents\cs1113\Labs>java Lab4b < lab4data.txt
Celsius | Fahrenheit
-----------+-----------
degreeCelsiusException in thread "main" java.util.MissingFormatArgumentEx
ception: Format specifier 'd'
at java.util.Formatter.format(Formatter.java:2487)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Lab4b.main(Lab4b.java:57)
So either I am having trouble with the loop or the printf statement
Any hints/tips on those would be great
Here is the txt file it has to read in:
5.5
15.2
25.4
35.8
45.1
55.9
65.3
75.7
85.6
95.0
And here is the print format is has to look like:
command in cmd: java Lab4b < lab4data.txt
Celsius | Fahrenheit
-----------+-----------
5.50| 41.90
15.20| 59.36
25.40| 77.72
35.80| 96.44
45.10| 113.18
55.90| 132.62
65.30| 149.54
75.70| 168.26
85.60| 186.08
95.00| 203.00
^^^^The lines have to match up^^^^