-1

I have an assignment due for university regarding a spellchecker program that uses a dictionary text file provided to us with one word a line for a couple hundred words. I have thus been experimenting with self made simple reading files(chinese.txt) in code for only the duration required instead of the entire dictionary at the start. I made two classes one with the read file function and one to execute it (spellchecker). however when i try to execute i keep getting the same error regardless of what i change saying exception in thread.

readfile.java:

package coursework2;
import java.io.*;
import java.util.*;

public class readfile {

private Scanner x;

public void openFile() {
    try {
        x = new Scanner(new File("chinese.txt"));
    }
    catch(Exception e) {
        System.out.println("Not found");
    }
}
public void readFile() {
    while(x.hasNext()) {
        String a = x.next();
        String b = x.next();
        String c = x.next();

        System.out.printf("%s %s %s", a,b,c);
    }
}
public void closeFile() {
    x.close();
}

spellchecker.java:

epackage coursework2;

class spellchecker {

public static void main(String[] args) {

    readfile r = new readfile();
    r.openFile();
    r.readFile();
    r.closeFile();
}

}

text file (chinese) just has a couple words 3 per line hence the formatting of the print method. if anyone can let me know how to get rid of this error or a better method to start on my approach to this spell checker program it would be greatly appreciated. As my actual project requires a use of a dictionary which has a couple thousand variables probably.

Kind Regards

1st year java learner.

  • epackage is not in the code but a typo posting this question. – Masroor Qureshi Dec 17 '19 at 22:54
  • 2
    When you ask about an exception, start by reading the type of the exception and its message. Then where this exception is thrown. Then the javadoc of the exception and the javadoc of the method throwing it. Not posting the exception stack trace leaves us in the dark. That's where you (and we) must start the investigation. – JB Nizet Dec 17 '19 at 22:55
  • 2
    `"i keep getting the same error"` Which is? – Nuno Palma Dec 17 '19 at 23:12
  • `catch(Exception e) System.out.println("Not found");` Some advice: Always log your exceptions, except in rare special cases. You're hiding what the actual problem is by not logging it. In schoolwork like this, it's probably fine to do `e.printStackTrace()`. I would not use `e.getMessage()` exclusively, as that doesn't give you a stack trace. In real-world projects, you'd use a logging library. Maybe something like log4j. – Christopher Schneider Dec 18 '19 at 16:19
  • Exception in thread "main" Exception Raised: chinese.txt (The system cannot find the file specified) java.lang.NullPointerException at coursework2/coursework2.readfile.readFile(readfile.java:21) at coursework2/coursework2.spellchecker.main(spellchecker.java:9) – Masroor Qureshi Dec 18 '19 at 17:28

1 Answers1

0

As others have said, in the comments, you need to tell us what the exception is. Change the openFile function as follows:

public void openFile() 
{
    try 
    {
        x = new Scanner(new File("chinese.txt"));
    }
    catch(Exception e) 
    {
        System.out.println("Exception Raised: " + e.getMessage());
    }
}

Re-run it and tell us what it printed out. If the problem isn't inside openFile(), place an exception handler around the code inside readFile() in the same manner as illustrated above and tell us what it prints out there.

hermit
  • 56
  • 6
  • Exception in thread "main" Exception Raised: chinese.txt (The system cannot find the file specified) java.lang.NullPointerException at coursework2/coursework2.readfile.readFile(readfile.java:21) at coursework2/coursework2.spellchecker.main(spellchecker.java:9) – Masroor Qureshi Dec 18 '19 at 17:22
  • the file name is not incorrect nor is it missing. its located in the same folder as the programs spellchecker and read file(labeled coursework2) and its the same format, so im not sure what i can change. the only other option that pops up is nullpointereception menu – Masroor Qureshi Dec 18 '19 at 17:26
  • I just ran your code and it worked fine. All I can think of is to specify a full path and file name for chinese.txt. – hermit Dec 19 '19 at 06:56