Java 如何从.txt文件读取填充的数组?

Java 如何从.txt文件读取填充的数组?,java,arrays,file-io,try-catch,Java,Arrays,File Io,Try Catch,我有一个.txt文件,里面有87个四个字母的单词。我基本上是在制作一个刽子手游戏,在这个游戏中,用户开始游戏,并在0到86之间生成一个随机数,然后程序转到.txt文件,并选择该单词供用户猜测。这是我第一次尝试这样做,因为我是一名编程新手,我正在使用的这本书几乎没有任何帮助。所以…以下是我为此所做的: class ReturnRandomInteger { public int randomInteger() { int randomInt = 0; fo

我有一个.txt文件,里面有87个四个字母的单词。我基本上是在制作一个刽子手游戏,在这个游戏中,用户开始游戏,并在0到86之间生成一个随机数,然后程序转到.txt文件,并选择该单词供用户猜测。这是我第一次尝试这样做,因为我是一名编程新手,我正在使用的这本书几乎没有任何帮助。所以…以下是我为此所做的:

class ReturnRandomInteger {

    public int randomInteger() {
        int randomInt = 0;
        for (int i = 0; i < 1; i++) {
        randomInt = (int) (Math.random() * 86) + 0;
    }
return randomInt;
}
    public static String getWord(int randomInt, String line) {
        FourLetterWords newObject = new FourLetterWords();
        if (randomInt == String line)

    }
}

class FourLetterWords {

    public static void readFile() {

    try {
        String fileName = "C//FourLetterWords.txt";
        File fourLetterWords = new File(fileName);
        Scanner in = new Scanner(fourLetterWords);

        ArrayList<FourLetterWords> words = new ArrayList<>();

        while (in.hasNextLine()) {
            String line = in.nextLine();
        }

    } catch (FileNotFoundException ex) {
    System.out.println("File not found.");
    }
}
}
类返回随机整数{
公共整数{
int-randomInt=0;
对于(int i=0;i<1;i++){
randomInt=(int)(Math.random()*86)+0;
}
返回随机数;
}
公共静态字符串getWord(int randomInt,字符串行){
FourLetterWords newObject=新的FourLetterWords();
if(randomInt==字符串行)
}
}
第四类字母词{
公共静态void readFile(){
试一试{
String fileName=“C//fourlettwords.txt”;
File fourLetterWords=新文件(文件名);
扫描仪输入=新扫描仪(四个字母单词);
ArrayList words=新的ArrayList();
while(在.hasNextLine()中){
String line=in.nextLine();
}
}捕获(FileNotFoundException ex){
System.out.println(“未找到文件”);
}
}
}
首先,我不知道我的尝试是否正确。文件名为fourlettwords.txt。如果是错误的,我想知道读取.txt文件中每一行的正确方法是什么。现在在我的ReturnRandomInteger类中,我从我的FourLetterWords类中创建了一个新的对象,你可以看到我正在尝试做的是让它成为这样,如果用户生成的随机整数等于.txt文件中的对应行,那么它将在该行中返回该单词。显然,int不能等于字符串,那么我应该如何将生成的整数与.txt文件中的单词对应起来呢


如果能得到任何帮助,我将不胜感激。

创建一个
字符串数组
(假设您将其命名为
words[]
)。在程序开始时,从文件中逐个读取行,并将它们存储在数组中(
words[i++]=line
)。现在,当您需要选择一个单词时,只需执行
word=words[randomInt]
创建一个
字符串数组(假设您将其命名为
words[]
)。在程序开始时,从文件中逐个读取行,并将它们存储在数组中(
words[i++]=line
)。现在,当您需要选择一个单词时,只需执行
word=words[randomInt]
创建一个
字符串数组(假设您将其命名为
words[]
)。在程序开始时,从文件中逐个读取行,并将它们存储在数组中(
words[i++]=line
)。现在,当您需要选择一个单词时,只需执行
word=words[randomInt]
创建一个
字符串数组(假设您将其命名为
words[]
)。在程序开始时,从文件中逐个读取行,并将它们存储在数组中(
words[i++]=line
)。现在,当您需要选择一个单词时,只需执行以下操作:

    String fileName = "C:\\FourLetterWords.txt";
    File fourLetterWords = new File(fileName);
    Scanner in = new Scanner(fourLetterWords);

    ArrayList<String> words = new ArrayList<String>();

    while (in.hasNextLine()) {
        words.add(in.nextLine());
    }

试着这样做:

    String fileName = "C:\\FourLetterWords.txt";
    File fourLetterWords = new File(fileName);
    Scanner in = new Scanner(fourLetterWords);

    ArrayList<String> words = new ArrayList<String>();

    while (in.hasNextLine()) {
        words.add(in.nextLine());
    }

试着这样做:

    String fileName = "C:\\FourLetterWords.txt";
    File fourLetterWords = new File(fileName);
    Scanner in = new Scanner(fourLetterWords);

    ArrayList<String> words = new ArrayList<String>();

    while (in.hasNextLine()) {
        words.add(in.nextLine());
    }

试着这样做:

    String fileName = "C:\\FourLetterWords.txt";
    File fourLetterWords = new File(fileName);
    Scanner in = new Scanner(fourLetterWords);

    ArrayList<String> words = new ArrayList<String>();

    while (in.hasNextLine()) {
        words.add(in.nextLine());
    }

由于整个随机数部分几乎只有一行代码,并且getWord方法在逻辑上不应该在提供随机数的类中,所以我将一个类放在一起完成整个工作

我建议将文件名也设置为动态文件名(例如,向RandomWordProvider构造函数添加一个参数),但为了使其更简单,我决定不这样做

class RandomWordProvider {

    private List<String> words;

    public RandomWordProvider() {
        words = readFile();
    }

    private int randomInteger() {
        int randomInt = (int) (Math.random() * words.size());
        return randomInt;
    }

    public String getWord(int randomInt, String line) {
        int randomPosition = randomInteger();
        String randomWord = words.get(randomPosition);
        return randomWord;
    }

    private List<String> readFile() {

        List<String> wordsList = new ArrayList<>();

        try {
            String fileName = "C:/FourLetterWords.txt";
            File fourLetterWords = new File(fileName);
            Scanner in = new Scanner(fourLetterWords);

            while (in.hasNextLine()) {
                String line = in.nextLine();
                if (line!=null && !line.isEmpty()) {
                    wordsList.add(line);
                }
            }
        } catch (FileNotFoundException ex) {
            System.out.println("File not found.");
        }

        return wordsList ;
    }
}
类提供程序{
私人列表词;
公共提供程序(){
words=readFile();
}
私有整数{
int randomInt=(int)(Math.random()*words.size());
返回随机数;
}
公共字符串getWord(int randomInt,字符串行){
int randomPosition=randomInteger();
字符串randomWord=words.get(randomPosition);
返回随机字;
}
私有列表读取文件(){
List wordsList=new ArrayList();
试一试{
String fileName=“C:/fourlettwords.txt”;
File fourLetterWords=新文件(文件名);
扫描仪输入=新扫描仪(四个字母单词);
while(在.hasNextLine()中){
String line=in.nextLine();
if(line!=null&&!line.isEmpty()){
添加(行);
}
}
}捕获(FileNotFoundException ex){
System.out.println(“未找到文件”);
}
返回单词列表;
}
}
我删除了对getWord的静态访问,因此您必须初始化对象,并且首先读取文件

随机整数: 我更改了函数来计算它,因此它需要列表的大小,并且如果以后要添加更多单词,它是灵活的。 方法Math.random()永远不会返回1,因此对于87个元素,数字将介于0和(在您的示例中)86.999之间。。。其中,转换为int将只删除小数部分

getWord: 该方法获取一个随机值作为单词列表中的位置指示器。 然后,它从列表中取出随机单词,并最终返回

读取文件: 与您已经做的差不多,我添加了一个空字符串和空字符串检查,然后将单词添加到稍后返回的数组中。 据我所知,Java并不太关心路径中使用的斜杠,我从依赖操作系统的路径切换到始终在某一点使用“普通”斜杠

这是干编码的,所以我希望我没有遗漏任何东西,尤其是我没有使用扫描仪检查阅读这些行的整个部分…:) 为了让它更容易理解,我决定再写一些