Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:44:11+00:00 2026-06-10T07:44:11+00:00

I’m having an issue with my programs construction. I can’t seem to find where

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:44:13+00:00Added an answer on June 10, 2026 at 7:44 am

    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
               }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.