保存文件内容到一个数组 [英] Storing file content into an array
问题描述
我有我的刽子手程序出现了问题。我真的觉得我需要做的是超出了我的理解有关Java。这里是我的code
I'm having a problem with my hangman program. I really think what I need to do is beyond what I understand about java. Here's my code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Random;
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 = new String[25];
final Random generator = new Random();
Scanner keyboard = new Scanner(System.in); // to read user's input
System.out.println("Welcome to Nick Carfagno's 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())
{
}
}
finally
{
if (wordScan != null)
{
wordScan.close();
}
}
// get random word from array
class pickRand
{
public String get(String[] wordList)
{
int rnd = generator.nextInt(wordList.length);
return wordList[rnd];
}
}
System.out.println(wordList);
}
}
我能得到的程序读取一个文件,然后打印到屏幕,但我无法弄清楚如何从文件中的单词存储到一个数组。我不是在所有先进的,所以请尽量和尽可能彻底。
I was able to get the program to read a file and then print to screen, but I can't figure out how to store the words from file into an array. I not advanced at all, so please try and be as thorough as possible.
推荐答案
您需要保存在字符串
对象读线,并将其分配到的某一领域数组。例如:
You need to save the read line in a String
object and assign it to a certain field of the array. For example:
wordList[0] = myString;
这将的myString
的值分配给你的数组的第一个字段。
This would assign the value of myString
to the first field of your array.
这篇关于保存文件内容到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!