0

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");
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    If the file exists, then the problem is that your path to the file is wrong. Please check out the detailed answer in the duplicate to find out more on why this happens and how to fix it. Also note that in general you're better off using resources and not files. – Hovercraft Full Of Eels Mar 09 '18 at 19:45
  • Your file cannot be reached. – zlakad Mar 09 '18 at 19:46

0 Answers0