0

I am trying to load a text file into java to get each individual word stored in an ArrayList of words. (which is an object that I have created that is working without problems.

import java.util.*;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class txtUtils
{
public ArrayList<Word> readFromText(ArrayList<Word> Words)
{
    String file = "corpus.txt";
    FileInputStream in = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine;

        while ((strLine = br.readLine()) != null) {
            for (int j = 0; j < strLine.length(); j++){
                {         
                    strLine = br.readLine();
                    int start = 0;
                    for(int x= start; x < strLine.length(); x++)
                    {
                        if(strLine.charAt(x)== ' ' || strLine.charAt(x) == '.')
                        {     
                            Words.add(new Word((strLine.substring(start,x- 1)).toUpperCase()));
                            start = x;
                        }
                    }
                }
            }

        }   
    in.close();
    return Words;

}
}

When compiling it, I get this error.

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

It is shown for the line, FileInputStream in = new FileInputStream(file);

The file is clearly there and has even been added in my classpath, so I do not see why it keeps on throwing this exception. Is there a way to override it? If not, is there any other way to load a text file into java?

Buddhima Gamlath
  • 2,318
  • 1
  • 16
  • 26

4 Answers4

1

The exception's not being thrown, it's telling you that you need to put the code inside a try-catch block or have your function throw the exception.

Edit: So there was a -1 vote, but no explanation as to what was missing or so totally incorrect as to justify it though.

splrs
  • 2,424
  • 2
  • 19
  • 29
  • It seems that the OP is having a bit of trouble with understanding how exceptions work in java, so some kind of more specific help or a useful link would have been good. – Daisy Holton Jan 01 '14 at 18:32
  • But that doesn't justify a downvote, as the information I've provided is accurate. The appropriate action in the case that is _no_ vote. It's hardly misleading info is it? – splrs Jan 01 '14 at 18:34
1
public ArrayList<Word> readFromText(ArrayList<Word> Words) throws FileNotFoundException

Is known as 'declaring' the exception. The method declares that this exception may be thrown.

It would be better in this situation to declare it, than do a try/catch. This is because your class is at a lower level and higher level classes, such as a class that would use this one, would be more suited for the try/catch.

More generally, see:

Daisy Holton
  • 533
  • 3
  • 17
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • At a guess, I'd suggest there's one guy in here who's answered and downvoted everyone else. – splrs Jan 01 '14 at 18:36
0

Your problem isn't happening at runtime, it's happening at compile time. The compiler is warning you because it doesn't know what to do if there isn't a file.

In java, exceptions can be caught or thrown. To catch the exception, use a statement like this around your code:

try
{
    FileInputStream in = new FileInputStream(file);
}
catch(FileNotFoundException e)
{
    e.printStackTrace();
}

This will allow your code to keep working even if the exception is thrown, and the e.printStackTrace() line will print out information about the exception.

Alternativley, you can throw the exception by adding a throws declaration to the method header like this: public ArrayList<Word> readFromText(ArrayList<Word> Words) throws FileNotFoundException

And then the exception will be thrown. This means that the exception will be ignored at compile time, and pushed up to the next layer. The problem with that is that when someone ends up using your txtutils class, they will have to also catch or throw the exception.

Daisy Holton
  • 533
  • 3
  • 17
0

Here you are getting a compile error. It is because you are calling some functions that could throw exceptions at run-time but you have not taken any precautions to handle it. Java does not do this for all types of exceptions.

Here's a good explanation of so called checked and unchecked Exceptions in java.

Checked vs Unchecked Exceptions in Java

In your case, you can overcome this issue in two ways.

One way is to enclose the statements around a try-catch block.

public ArrayList<Word> readFromText(ArrayList<Word> Words)
{
    try{
        String file = "corpus.txt";
        FileInputStream in = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        // other program logic...

        in.close();
        return Words;
    }catch(Exception e){
        // do something if error occurs...
        return null;  // or an empty ArrayList...
    }
}

For more information about handling exceptions, refer this.

Catching and Handling Exceptions

The other way is to declare the method as a one that throws Exceptions.

public ArrayList<Word> readFromText(ArrayList<Word> Words) throws Exception
{
   // method logic goes here
}

But, if you do this, then whatever the methods that will be calling this method also should be declared like this, or they should handle the exception inside a try-catch block.

Community
  • 1
  • 1
Buddhima Gamlath
  • 2,318
  • 1
  • 16
  • 26