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();
}
}