I’m having an issue with my programs construction. I can’t seem to find where or why I need to put my constructor in. I can’t tell if it is in there, or not. Anyway, here is the main code:
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HangmanProject
{
public static void main(String[] args) throws FileNotFoundException
{
public static void getFile() {
getFile gf() = new getFile();
Scanner test = gf.wordScan;
}
}
So that’s the main program but it calls this one:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class getFile
{
String wordList[] = new String[10]; // array to store all of the words
int x = 0;
Scanner keyboard = new Scanner(System.in); // to read user's input
System.out.println("Welcome to Hangman Project!");
// Create a scanner to read the secret words file
Scanner wordScan = null;
try {
wordScan = new Scanner(new BufferedReader(new FileReader("words.txt")));
while (wordScan.hasNext()) {
wordList[x] = wordScan.next();
System.out.println(wordList[x]);
x++;
}
}
finally {
if (wordScan != null)
{
wordScan.close();
}
}
}
My questions are:
- where is my constructor,
- am I using it correctly,
- should my layout be changed?
My instructor is telling me that “I still do not see the constructor method in your class where you should be initializing the instance variables for your class. You cannot just put code inside a class.” I really don’t understand what that means.
First, I made a few modifications to your program. Your first big error was that you have a class called getFile, and also a method called getFile. Keep method names as method names, but class names are usually capitalized. I changed it to GetFile.
OK; now onto the actual class itself.