繁体   English   中英

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

[英]How do I read a populated array from a .txt file?

我有一个.txt文件,其中包含87个四个字母的单词。 我实质上是在制作一个a子手游戏,在该游戏中,用户开始游戏,并在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.");
    }
}
}

对于初学者,我不知道我的尝试和追捕是否正确。 该文件的名称是FourLetterWords.txt。 如果不正确,我想知道读取.txt文件中每一行的正确方法是什么。 现在,在我的ReturnRandomInteger类中,我从FourLetterWords类中创建了新对象,您可以看到我正在尝试做的是拥有该对象,以便如果用户生成的随机整数等于.txt文件中的相应行,则它将返回该行中的单词。 显然,int不能等于String,那么我应该如何对应生成的.txt文件中的单词生成的整数?

我将不胜感激。

尝试这样的事情:

    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());
    }

现在,您的单词将存储在单词arraylist中,您可以使用以下随机数进行访问:

    words.get(randomNumber);

由于整个随机数部分几乎只包含一行代码,因此从逻辑上讲,getWord方法不应位于提供随机数的类中,我将一个类放在一起即可完成全部工作。

我建议也使fileName动态化(例如,向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 ;
    }
}

我删除了对getWord的静态访问,因此您必须初始化该对象,并且最初会读取该文件。

randomInteger:我更改了函数以对其进行计算,因此它占用列表的大小,并且如果您以后要添加更多单词,它很灵活。 Math.random()方法将永远不会返回1,因此对于您的87个元素,该数字将介于0和(在您的示例中)86.999 ...之间,其中将类型强制转换为int只会截断小数部分。

getWord:该方法获取一个随机值作为单词列表中的位置指示符。 然后,它将随机单词从您的列表中删除,最后将其返回。

readFile:差不多您已经做了,我添加了一个用于检查null和空Strings的选项,然后将单词添加到稍后返回的数组中。 据我所知,Java不太关心您在路径中使用什么斜杠,所以我从依赖于OS的路径切换到始终始终使用``正常''斜杠...

这是经过干燥编码的,所以我希望我不会错过任何东西,尤其是我没有检查整个使用Scanner读取行的内容... :)为了使它更容易理解,我决定编写一些不必要的代码行,randomInteger()和getWord()可以在一行中完成;)

创建一个Strings数组(假设您将其命名为words[] )。 在程序启动时,从文件中一行一行地读取行,并将其存储在数组中( words[i++]=line )。 现在,当您需要选择一个单词时,只需执行word = words[randomInt]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM