Skip to content
Advertisement

How can I create and populate a constant from a file? [closed]

I’m making a word game program which has a dictionary of words in a text file. I need to run 2 methods through the dictionary in a single run by having a user choose options 1 and 2, then 3 to close. I need to make the dictionary a constant called DICTIONARY which is new to me.

Currently, in my program I have just opened the file and printed the contents before displaying the menu which option 1 and 2 are the methods that will play a small game which I am yet to code. Before I can start method 1 and 2 I need to create the DICTIONARY constant.

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

public class Small_Programming_Assignment {

    public static void main(String[] args) throws FileNotFoundException  {
        String fileName = "dictionary.txt";
        File file = new File(fileName);
        if (!file.isFile()) {
            System.out.println("Dictionary file cannot be opened");
            System.exit(0);
        }

        Scanner input = new Scanner(file);
        while (input.hasNextLine()) {
            System.out.println(input.nextLine());
        }
        
        getSelection();
        substringProblem();
        pointsProblem();
    }

    public static void getSelection() {
        Scanner sc = new Scanner(System.in);
        System.out.println("");
        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 selection: ");
        
        int choice = 0;
        
        try {
            choice = sc.nextInt();
        } 
        catch(InputMismatchException e) {
            System.out.println("Invalid option. Try again.");
            getSelection();
        }

        if (choice == 1) {
            substringProblem();
        } 
        else if (choice == 2) {
            pointsProblem();
        } 
        else if (choice == 3) {
            System.out.println("Goodbye!");
            System.exit(0); 
        } 
        else {
            System.out.println("Invalid option. Try again.");
            getSelection();
        }
    }

    public static void substringProblem()  {
        System.out.println("Substring Problem");
        getSelection();
    }

    public static void pointsProblem() {
        System.out.println("Points Problem");
        getSelection();
    }
}

Advertisement

Answer

static and final key words can be used on a class variable to make it behave like a constant.

  1. The static modifier causes the variable to be available without an instance of it’s defining class being loaded

  2. The final modifier makes the variable unchangeable

For example:

    private static final String FILE_NAME = "dictionary.txt";
    private static final File DICTIONARY = new File(FILE_NAME);

    public static void main(String[] args) throws FileNotFoundException  {

        if (!DICTIONARY.isFile()) {
            System.out.println("Dictionary file cannot be opened");
            System.exit(0);
        }
        Scanner input = new Scanner(DICTIONARY);
        while (input.hasNextLine()) {
            System.out.println(input.nextLine());
        }

        getSelection();
        substringProblem();
        pointsProblem();


    }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement