0

I'm trying to get a method to check for the existence of a file, and print a message that the file doesn't exist, but it also has to be a class variable rather than an instance variable.

I had it working when it was only in subString method, was not a class variable and without infix/suffix/prefix code.

Here is my code. It's still a little bit messy and no conforming to formatting convention.

Appreciate your help.

package wordgames;

import java.io.File;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;

public class WordGames {

    private static final String DICTIONARY = "dictionary.txt";
    private static String[] wordsCollection;
    private static int wordCount = 0;

    public static void main(String[] args) throws Exception {
        wordsCollection = new String[100];
        File fileReader = new File(DICTIONARY);
        Scanner fileScanner = new Scanner(fileReader);

        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            wordsCollection[wordCount] = line;
            wordCount++;
        }

        getSelection();
    }

    static String getSelection() throws FileNotFoundException {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Welcome to the Word Games program menu.");
        System.out.println("Select from one of the following options.");
        System.out.println("1. Substring problem.");
        System.out.println("2. Points problem.");
        System.out.println("3. Exit.");
        System.out.println("Enter your selections: ");
        String selection = keyboardInput.next();

        switch (selection) {

        case "1":
            subStringProblem();
        case "2":
            pointsProblem();
        case "3":
            System.out.println("Good Bye!");
            System.exit(0);
        default:
            System.out.println("Invalid option.  Try again.");
            getSelection();
        }

        return null;
    }

    static void subStringProblem() throws FileNotFoundException {
        File fileReader = new File("DICTIONARY.txt");
        Scanner fileScanner = new Scanner(fileReader);
        if (fileReader.isFile() == true) {
        } else {
            System.out.println("File doesn't exist.  Exiting.");
            System.exit(0);
        }
        System.out.println("Substring problem.");
        System.out.println("Enter a Substring:");
        Scanner keyboardInput = new Scanner(System.in);
        String subString = keyboardInput.next();
        System.out.print(subString);
        System.out.println();
        String notFound = " - not found";
        String infixFound = " - infix";
        String prefixFound = " - prefix";
        String suffixFound = " - suffix";

        for (int i = 0; i < wordCount; i++) {
            String temp = wordsCollection[i];

            boolean found = false;

            if (wordsCollection[i].startsWith(subString)) {
                found = true;
                temp = temp + prefixFound;
            }

            if (wordsCollection[i].endsWith(subString)) {
                found = true;
                temp = temp + suffixFound;
            }

            if (wordsCollection[i].contains(subString)) {
                found = true;
                temp = temp + infixFound;
            }

            if (!found) {
                System.out.printf(" " + wordsCollection[i] + notFound + "\n");
            } else {
                System.out.printf(" " + temp + "\n");
            }

        }

        getSelection();
    }

    private static void pointsProblem() throws FileNotFoundException {
        System.out.println("Points problem.");
        getSelection();
    }
}
    
Hulk
  • 6,399
  • 1
  • 30
  • 52
Nad Deb
  • 29
  • 4
  • does this help https://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java – newbie Oct 08 '20 at 07:28
  • Just make `fileScanner` a member of the class instead of a local variable in method `subStringProblem()`, i.e. `private static Scanner fileScanner;` – Abra Oct 08 '20 at 07:40
  • also, the `switch` in your `getSelection()` method has no `break` or return statements - are you sure you always want to call `System.exit(0)` except for the default case? – Hulk Oct 08 '20 at 07:48
  • @Abra, Hulk and Newbie. Thank you, I've fixed this. – Nad Deb Oct 09 '20 at 02:58

1 Answers1

1

I've noticed that you have put throws FileNotFoundException in the signature of all of your functions. You only do this if you don't want to catch the exception in that function, and want the caller to be responsible for handling the exception. In your program, that is never the case, you always want to handle it immediately by exiting, so remove it from everywhere.

The part of your code that is crashing:

File fileReader = new File("DICTIONARY.txt");
Scanner fileScanner = new Scanner(fileReader);
if (fileReader.isFile() == true) {
else {
  System.out.println("File doesn't exist.  Exiting.");
  System.exit(0);
}

This code will work if you remove the fileScanner. It throws an exception if the file doesn't exist, but since you're not actually using it for anything, you can just remove it. The fileReader.isFile() check will then do the job. (fileReader.exists() is closer to)

The corrected code:

File fileReader = new File("DICTIONARY.txt");

if (fileReader.exists() == false) {
  System.out.println("File doesn't exist.  Exiting.");
  System.exit(0);
}

You're also reading a file in the main() function. There you're actually using the fileScanner, so this gives you a choice of either using the same check as above, or you can actually catch the FileNotFoundException this time, you could try that at least to give it a try.

try {
    File fileReader = new File(DICTIONARY);
    Scanner fileScanner = new Scanner(fileReader);

    while (fileScanner.hasNextLine()) {
        String line = fileScanner.nextLine();
        wordsCollection[wordCount] = line;
        wordCount++;
    }
} catch (FileNotFoundException e) {
    System.out.println("File doesn't exist. Exiting.");
    System.exit(0);
}

It would be good practice if within the try/catch block you actually called a function, that would make stop it from looking ugly.

zOqvxf
  • 1,469
  • 15
  • 18
  • Thank you for your help. I've made the fileReader a member of the class rather than local variable, and I'm using the corrected code, and this prints out "File doesn't exist. Exiting.", however, I would like to if possible have it check for the file exist once it enters the substring problem method. Do I need to make a separate method for file exists? I've entered the breaks into the switch case also. – Nad Deb Oct 09 '20 at 02:54
  • I believe the code already does what you're asking. You can use this to check if the file exists: `if (fileReader.exists() == false) { }`, not sure what more you're after. – zOqvxf Oct 09 '20 at 10:39