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 :
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"));