javalayoutsyntaxconstructorinitialization

How to use a constructor in Java?


I'm having an issue with my programs using a constructor. 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:

My instructor is telling me this:

"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.


Solution

  • 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.

    import java.util.*;
    import java.io.*;
    public class HangmanProject{
       public static void main(String[] args) throws FileNotFoundException{ //this is the first thing that runs, the main method
          GetFile gf = new GetFile();  //this will create the getFile object
          try {
             gf.getWords(); //runs the method that we make later
          }
          catch(FileNotFoundException potatoes) {   //this will print out error message if there is no words.txt
             System.out.println(potatoes.getMessage());
          }                                
        } //closes main method
    }
    

    OK; now onto the actual class itself.

    import java.util.*;
    import java.io.*;
    public class GetFile {        
        //Here are your variables
        private String[] wordList = new String[10];   
        private int x = 0;                             
        //good practice to have variables private;
        //Now it is time for the constructor.
        public GetFile() 
           { 
         //empty constructor; you don't need to have any instance variables declared.
           } 
        public void getWords() throws FileNotFoundException {  //the big method for doing stuff
            Scanner keyboard = new Scanner(System.in);    // to read user's input
            System.out.println("Welcome to Hangman Project!");
            Scanner wordScan = null;
            wordScan = new Scanner(new File("words.txt"));
            while (wordScan.hasNext()) { //checking if there are more words
                wordList[x] = wordScan.next();  
                System.out.println(wordList[x]);  //print them out as the array is filled
                x++;  
                }
            if (wordScan != null)
               {
               wordScan.close(); //close the file after you are finished
               }
        }
    }