Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(beginner help)The system cannot find the file specified

public static void main(String[] args) throws FileNotFoundException {
    int num1;
    double num2;
    String name;
    Scanner in =  new Scanner(new File("in.txt"));
    num1 = in.nextInt();
    num2 = in.nextDouble();
    name = in.next();
    System.out.printf("Hi %s, the sum of %d and %.2f is %.2fn", name, num1, num2, num1 + num2);
    in.close();
}

I have created a notepad named "in.txt" next to the java file and gave me this error:

Exception in thread "main" java.io.FileNotFoundException: in.txt (The system cannot find the file specified)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
        at java.base/java.util.Scanner.<init>(Scanner.java:639)
        at TextFileScannerWithThrows.main(TextFileScannerWithThrows.java:10)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I assume you are using linux or mac. You could change the file path to the one relative to the src directory as below.
Please change "src/main/java/package" to your local directory. Good luck!

Scanner in =  new Scanner(new File("src/main/java/package/in.txt"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading