-1

I am trying to run this simple program that reads from a separate text file and prints out each line. However, when I try to compile it, it keeps giving me the same error:

story.java:11: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
                x = new Scanner(new File("names.txt"));

Here is my code:

import java.io.*;
import java.util.*;

public class story {

    private static Scanner x;

    public static void main(String[] args) {

        String story = "";
        x = new Scanner(new File("names.txt"));

        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);

    }
}
Shoaib Ahmed
  • 51
  • 1
  • 2
  • 4

5 Answers5

1

This message is telling you that your main() method is doing some stuff that may throw FileNotFoundException but you are neither catching this exception, nor declaring that such an exception may be thrown by the main() function.

To correct it, declare your main() method as follows:

public static void main(String[] args) throws FileNotFoundException {
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

When programming there is the normal flow of logic that solves your problem, and a second flow of logic that handled "Exceptional" unexpected situations. Java uses the type Exception and the keyword throw to have bits of code present exceptional error states.

Your program, when being complied, is being checked for its ability to handle exceptional return values. Since you didn't handle the exceptional return value, it is not going to be compiled.

Below is your code, handling this Exception. Pay close attention to the try and catch block structure, as it is how one writes code that can handle an Exception being raised. However, don't be constrained by how I handled the exception for you, because the details of how to handle the exception are dependent on what you would prefer to do.

import java.io.*;
import java.util.*;

public class story {

    private static Scanner x;

    public static void main(String[] args) {

        String story = "";
        try {
            x = new Scanner(new File("names.txt"));
        } catch (FileNotFoundException error) {
            System.out.println("the program failed because \"names.txt\" was not found.");
            return -1;
        }

        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);

    }
}

There is another approach to handling exceptions that sometimes is appropriate, which is to "not handle" the exception. When doing so, change the method from whatever the method was to whatever the method was throws Exception. For example public static void main(String[] args) to public static void main (String[] args) throws Exception.

While this handles the compiler's complaint it should be used sparingly because exceptions that bubble up through the nested method calls past the main method will crash the program, typically without easily readable output that might make the error easier for a human to fix.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

Change it to

public static void main(String[] args) throws Exception {

    String story = "";
    x = new Scanner(new File("names.txt"));

    while(x.hasNext()){
        story = story + x;
    }
    System.out.println(story);

}

OR

Put try catch in your code

public static void main(String[] args) {

    String story = "";
    try {
        x = new Scanner(new File("names.txt"));
        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);
    }
    catch(Exception e) {
        // handle 
    }
}
Smit Shilu
  • 345
  • 3
  • 17
0

Whenever working with a file object, you must handle the chance of a FileNotFoundException, which occurs when the file you specify does not exist.

to fix this, simply say throws Exception in you main header, or use a try/catch block.

public static void main(String[] args) throws Exception {

or

public static void main(String[] args) {

    String story = "";
    try 
    {
        x = new Scanner(new File("names.txt"));
        while(x.hasNext()){
            story = story + x;
        }
        System.out.println(story);
    }
    catch(Exception e) 
    {
        System.out.println("Error: File not found!");
    }
} 
Luke Thistlethwaite
  • 428
  • 1
  • 4
  • 17
0

1) You need to put them in the try catch block. 2) specify the path of "names.txt". 3) My code looks like:

String story = "";
       try {
         x = new Scanner(new File("/Users/Workspace/names.txt"));
         while(x.hasNext()){
            System.out.println(story = story+x.next());
         }
         System.out.println(story);
     } catch (FileNotFoundException e){
         e.printStackTrace();
     }
 }
Sameer
  • 11
  • 1
  • 6