2

I have tried everything I can find on the internet, and nothing seems to fix this. I am begging for help, tell me what I am doing wrong. I am brand new to Java. Thanks!

import java.util.Scanner;

class File_Scanner {    
    public static void read() {
        File credentials_file = new File("credentials.txt");
        Scanner file_reader = new Scanner(credentials_file);
        String[] users = new String[6];
        int index_counter = 0;

        try {
            while (file_reader.hasNextLine()) {
                users[index_counter] = file_reader.nextLine();
            }
            file_reader.close();

        } catch (Exception e) {
          System.out.println(e.getClass());
        }
    }
}

This is showing the following error

Unreported exception FileNotFoundException; must be caught or declared to be thrown
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
K. Kretz
  • 72
  • 1
  • 2
  • 10

5 Answers5

2

I would like to recommend to surround the whole read function with a try/catch block like the following.

import java.util.Scanner;

class File_Scanner{    
    public static void read(){
        try {
            File credentials_file = new File("credentials.txt");
            Scanner file_reader = new Scanner(credentials_file);
            String[] users = new String[6];
            int index_counter = 0;

            while (file_reader.hasNextLine()) {
                users[index_counter] = file_reader.nextLine();
            }

            file_reader.close();
        } catch (Exception e) {
          System.out.println(e.getClass());
        }
    }
}

The idea of try/catch is to avoid any error that might occur while running your program. In your case, Scanner file_reader = new Scanner(credentials_file); can throw an error if the credentials_file is not found or deleted. Hence you need to cover this around a try block which will give you an exception which can be handled to show proper response message in the catch block.

Hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
2

I agree with the other answers as to the problem (the Scanner can throw an exception if it can't find the file). I haven't seen what I'd consider the correct solution though.

    String[] users = new String[6];
    int index_counter = 0;

    File credentials_file = new File("credentials.txt");
    try (Scanner file_reader = new Scanner(credentials_file)) {
        while (file_reader.hasNextLine()) {
            users[index_counter] = file_reader.nextLine();
            index_counter++;
        }
    } catch (FileNotFoundException e) {
        // handle exception
    } catch (NoSuchElementException e) {
        // handle exception
    } catch (IllegalStateException e) {
        // handle exception
    }

The try-with-resources statement will automatically close the Scanner for you. This will work with any class that implements the AutoCloseable interface.

And in this case, it puts the statement within the scope of the try, so exceptions will be caught.

Your exception handling is questionable, so I didn't include it. But that's not really the point here. You can read more about Best Practices to Handle Exceptions in Java or How to Specify and Handle Exceptions in Java.

There is an argument that you should let the exception bubble up to the caller. This answer describes how to do that. But in this case, the caller doesn't really know how to handle a FileNotFoundException, because it doesn't know anything about the file. The file is defined in this method.

You could throw a different, more explanatory exception. Or you could handle the exception here by explaining what a credentials.txt is. Or fail over to a default. Or just log the exception, although that's questionable. If you do that, you should explain (in a comment) why that is sufficient.

I added a line to increment index_counter just because it seemed to be missing.

See also

mdfst13
  • 850
  • 8
  • 18
1

Java has checked exceptions. Which means that if the code within your method declares that it might possibly throw a particular exception, your method either needs to do one of the following:

Either (1) handle that exception:

public static void read() {
    try {
        // your method code
    } catch (FileNotFoundException ex) {
        // handle the exception
    }
}

Or (2) declare that it might possibly throw that exception:

public static void read() throws FileNotFoundException {
    // your method code
}
David
  • 208,112
  • 36
  • 198
  • 279
1

Just add this to your method declaration:

public static void read() throws FileNotFoundException

For ways to handle checked exceptions in java check this(Oracle Docs Java Tutorials):

You associate exception handlers with a try block by providing one or more catch blocks directly after the try block. No code can be between the end of the try block and the beginning of the first catch block.

try {

} catch (ExceptionType name) {

} catch (ExceptionType name) {

}

And this:

Sometimes, it's appropriate for code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception

public void writeList() throws IOException {

P.S. Also, it has been already discussed on stackoverflow, so maybe should be marked as duplicate.

Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
0

You should read the java checked exception

public static void read() {
    try {
        File credentials_file = new File("credentials.txt");
        Scanner file_reader;
        file_reader = new Scanner(credentials_file);
        String[] users = new String[6];
        int index_counter = 0;

        while (file_reader.hasNextLine()) {
            users[index_counter] = file_reader.nextLine();
        }
        file_reader.close();
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (Exception e) {
        System.out.println(e.getClass());
    }
}
Rmahajan
  • 1,311
  • 1
  • 14
  • 23