We are asked to modify the feet to meters program from week 2 to read from, and write to a file. The input file should be named “feet.txt” and the output file “meters.txt”.
The input file should contain just double values. The output file should have on each line the feet and meters, formatted to a single decimal. Example of a line in the output file: (assume 5.2 was read from the file) 5.2 feet coverts to 1.6 meters. I am getting the following error in the console:
Exception in thread "main" java.io.
FileNotFoundException: input.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:196)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:139)
at java.base/java.io.FileReader.<init>(FileReader.java:72)
at lab8p2.main(lab8p2.java:21)
The code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class feet_to_meters {
public static void main(String[] args) throws FileNotFoundException {
double feet, meters;
Scanner fileScanner = new Scanner(new File("feet.txt"));
PrintWriter out = new PrintWriter("meters.txt");
while(fileScanner.hasNextDouble()) {
// read feet
feet = fileScanner.nextDouble();
// calculate meters: feet * 0.305
meters = feet * 0.305;
//write meters
out.write(Double.toString(meters));
}
fileScanner.close();
out.close();
System.out.println("Output successfully written in file");
}
}