簡體   English   中英

想要從文本文件中隨機選擇單詞,而不是從文本文件中打印所有內容

[英]Want to randomly choose word from text file and instead printing everything from text file

我希望編譯器從文本中randomly選擇一個單詞,而不是從文本文件中打印所有內容。 現在下面的代碼正在打印文本文件中的所有內容。 我認為我的getWord方法有問題,因為當我從主函數調用getWord方法時error

public class TextFile {

        protected static Scanner file;
        protected static List<String> words;


        public TextFile(){
            words = openFile();
        }

        private List<String> openFile() {

            //List<String> wordList = new ArrayList<String>();

                try {
                    file = new Scanner(new File("words.txt"));

                } catch (FileNotFoundException e) {
                    System.out.println("File Not Found");
                } catch (Exception e) {
                    System.out.println("IOEXCEPTION");
                }

                return words;
        }

        public void readFile() throws FileNotFoundException {

            //ArrayList<String> wordList = new ArrayList<String>();

            while(file.hasNext()){
                String a = file.nextLine();
                //Collections.shuffle(words);
                //String pickWord = words.get(1);
                //String[] a = 
                System.out.println(a);
            }
        }

        public void closeFile() {
            file.close();
        }

        public String getWord() {

            Random r = new Random(words.size());
            String randomWord = words.get(r.nextInt());
            //System.out.println(randomWord);

            return randomWord;
        }

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

            try {

                TextFile file = new TextFile();

                file.openFile();
                file.readFile();

                file.closeFile();

            } catch (Exception e) {
                System.out.println("IOEXCEPTION");
            }
        }
    }

在 openfile 方法中,您正在重新調整一個“word”變量,該變量為空,為變量賦值

錯誤來自 {getword();} 因為您正在訪問空變量的屬性,這是一個錯誤

     public List<String> readFile() throws FileNotFoundException {
       while(file.hasNext()){
            String a = file.nextLine();
            words.add(a);
            System.out.println(a);
        }
       return words;
    }

在返回語句行的打開文件方法中調用“return readfile();” 並嘗試您的代碼\\

無需在 main 方法中調用 readfile 方法

您在調用 getWord 方法時遇到異常,因為它在String randomWord = words.get(r.nextInt());行處拋出IndexOutOfBoundsException .

getWord方法的 PFB 修正:

public String getWord() {
    //You can use any approach..Random or Collections
    //Random r = new Random();      
    //String randomWord = words.get(r.nextInt(words.size()));

    Collections.shuffle(words);
    String randomWord = words.get(1);

    return randomWord;
}

同樣,您應該正確填充words字段:

public void readFile() throws FileNotFoundException {

    words = new ArrayList<String>();

    while (file.hasNext())
        words.add(file.nextLine());
}

嘗試這個。 您不需要在 main 中使用 getWord() 方法。 此外,為該類創建一個構造函數:

public TextFile() {
}

您的 openFile() 方法不需要返回字符串。

    private void openFile() {


          try {
                file = new Scanner(new File("words.txt"));

            } catch (FileNotFoundException e) {
                System.out.println("File Not Found");
            } catch (Exception e) {
                System.out.println("IOEXCEPTION");
            }
}

這是您的 readFile() 方法:1)讀取文件 2)將一行單詞拆分為每個單詞並將其放入數組 3)然后,隨機單詞

public void readFile() throws FileNotFoundException {

    //  List<String> wordList = new ArrayList<String>();


        while(file.hasNext()){


            String line = file.nextLine(); //read file one line at a time

            String[] parseWords = line.split(" "); //Parse what you read


            int index = new Random().nextInt(parseWords.length);

            String randW = parseWords[index];
            System.out.println(randW);

        }
    }

在您的主要方法中:

TextFile file1 = new TextFile ();

file1.openFile();
file1.readFile();
file1.closeFile();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM