有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java希望从文本文件中随机选择word,而不是从文本文件中打印所有内容

我希望编译器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");
            }
        }
    }

共 (3) 个答案

  1. # 1 楼答案

    在openfile方法中,您正在重新调整一个“word”变量,该变量是 null为变量赋值

    错误来自{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();”的open file方法中试试你的代码

    不需要在main方法中调用readfile方法

  2. # 2 楼答案

    调用getWord方法时,在第String randomWord = words.get(r.nextInt());行抛出IndexOutOfBoundsException时出现异常

    PFB对getWord方法的修正:

    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());
    }
    
  3. # 3 楼答案

    试试这个。不需要在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();