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 8426775
In Process

The Archive Base Latest Questions

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

I need to have this file print to an array, not to screen.And yes,

  • 0

I need to have this file print to an array, not to screen.And yes, I MUST use an array – School Project – I’m very new to java so any help is appreciated. Any ideas? thanks

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

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

        String scoreKeeper;     // to keep track of score
        int guessesLeft;        // to keep track of guesses remaining
        String wordList[];    // array to store words


        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()) {
                System.out.println(wordScan.next());
            }
        } finally {
            if (wordScan != null) {
                wordScan.close();
            }
        }
    }
}
  • 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-10T04:37:12+00:00Added an answer on June 10, 2026 at 4:37 am

    Nick, you just gave us the final piece of the puzzle. If you know the number of lines you will be reading, you can simply define an array of that length before you read the file

    Something like…

    String[] wordArray = new String[10];
    int index = 0;
    String word = null; // word to be read from file...
    // Use buffered reader to read each line...
        wordArray[index] = word;
        index++;
    

    Now that example’s not going to mean much to be honest, so I did these two examples

    The first one uses the concept suggested by Alex, which allows you to read an unknown number of lines from the file.

    The only trip up is if the lines are separated by more the one line feed (ie there is a extra line between words)

    public static void readUnknownWords() {
    
        // Reference to the words file
        File words = new File("Words.txt");
        // Use a StringBuilder to buffer the content as it's read from the file
        StringBuilder sb = new StringBuilder(128);
    
        BufferedReader reader = null;
        try {
    
            // Create the reader.  A File reader would be just as fine in this
            // example, but hay ;)
            reader = new BufferedReader(new FileReader(words));
            // The read buffer to use to read data into
            char[] buffer = new char[1024];
            int bytesRead = -1;
            // Read the file to we get to the end
            while ((bytesRead = reader.read(buffer)) != -1) {
    
                // Append the results to the string builder
                sb.append(buffer, 0, bytesRead);
    
            }
    
            // Split the string builder into individal words by the line break
            String[] wordArray = sb.toString().split("\n");
    
            System.out.println("Read " + wordArray.length + " words");
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    
    }
    

    The second demonstrates how to read the words into an array of known length. This is probably closer to the what you actually want

    public static void readKnownWords() 
    
        // This is just the same as the previous example, except we
        // know in advance the number of lines we will be reading    
        File words = new File("Words.txt");
    
        BufferedReader reader = null;
        try {
    
            // Create the word array of a known quantity
            // The quantity value could be defined as a constant
            // ie public static final int WORD_COUNT = 10;
            String[] wordArray = new String[10];
    
            reader = new BufferedReader(new FileReader(words));
            // Instead of reading to a char buffer, we are
            // going to take the easy route and read each line
            // straight into a String
            String text = null;
            // The current array index
            int index = 0;
            // Read the file till we reach the end
            // ps- my file had lots more words, so I put a limit
            // in the loop to prevent index out of bounds exceptions
            while ((text = reader.readLine()) != null && index < 10) {
    
                wordArray[index] = text;
                index++;
    
            }
    
            System.out.println("Read " + wordArray.length + " words");
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    
    }
    

    If you find either of these useful, I would appropriate it you would give me a small up-vote and check Alex’s answer as correct, as it’s his idea that I’ve adapted.

    Now, if you’re really paranoid about which line break to use, you can find the values used by the system via the System.getProperties().getProperty("line.separator") value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this file which I need to read the first bytes to check
I have this tag into my html file: {{block:my_test_block}} {{news:my_test_block2}} I need to parse
I have this function in my js file. I need to give time for
Using this file as source, I have a situation where I need to retrieve
I have some data and need to create a json file with this structure
I have text file with some text information and i need to split this
I have a file as documentRoot/app/webroot/myFile.php I need to be able to access this
I Have an Excel 2003 file with a line similar to this: I need
In the web page, I have file upload widget. I need to show this
I'm new to this symfony2 and twig so I'm not very familiar with it.

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.