Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 想要从文本文件中随机选择word,而不是打印文本文件中的所有内容吗_Java - Fatal编程技术网

Java 想要从文本文件中随机选择word,而不是打印文本文件中的所有内容吗

Java 想要从文本文件中随机选择word,而不是打印文本文件中的所有内容吗,java,Java,我希望编译器从文本中随机选择一个单词,而不是从文本文件中打印所有内容。现在,下面的代码正在打印文本文件中的所有内容。我认为我的getWord方法有问题,因为当我从主函数调用getWord方法时,我得到一个错误 public class TextFile { protected static Scanner file; protected static List<String> words; public TextFile(){

我希望编译器从文本中随机选择一个单词,而不是从文本文件中打印所有内容。现在,下面的代码正在打印文本文件中的所有内容。我认为我的
getWord
方法有问题,因为当我从主函数调用
getWord
方法时,我得到一个
错误

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");
            }
        }
    }
公共类文本文件{
受保护的静态扫描文件;
受保护的静态列表字;
公共文本文件(){
words=openFile();
}
私有列表openFile(){
//List wordList=new ArrayList();
试一试{
文件=新扫描仪(新文件(“words.txt”);
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}捕获(例外e){
System.out.println(“IOEXCEPTION”);
}
返回单词;
}
public void readFile()引发FileNotFoundException{
//ArrayList wordList=新建ArrayList();
while(file.hasNext()){
字符串a=file.nextLine();
//集合。洗牌(单词);
//字符串pickWord=words.get(1);
//字符串[]a=
系统输出打印项次(a);
}
}
公共文件(){
file.close();
}
公共字符串getWord(){
Random r=新的Random(words.size());
String randomWord=words.get(r.nextInt());
//System.out.println(随机字);
返回随机字;
}
公共静态void main(字符串[]args)引发FileNotFoundException{
试一试{
TextFile file=新的TextFile();
openFile();
readFile();
closeFile();
}捕获(例外e){
System.out.println(“IOEXCEPTION”);
}
}
}

在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;
    }
public List readFile()引发FileNotFoundException{
while(file.hasNext()){
字符串a=file.nextLine();
加上(a);
系统输出打印项次(a);
}
返回单词;
}
在返回语句行的openfile方法中,调用“return readfile();”并尝试您的代码\


无需在main方法中调用readfile方法

调用getWord方法时出现异常,因为它在
String randomWord=words.get(r.nextInt())行抛出
indexoutboundsException

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;
}
同样,您应该正确填写
单词
字段:

public void readFile() throws FileNotFoundException {

    words = new ArrayList<String>();

    while (file.hasNext())
        words.add(file.nextLine());
}
public void readFile()引发FileNotFoundException{
words=newarraylist();
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);

        }
    }

您的工作方向是正确的,
ArrayList
Collections.shuffle
会让workedIt打印每一行,因为您有一个“System.out.println(a);”在readFile方法中。此外,我在任何地方都没有看到对getWord方法的调用。“我希望编译器从文本中随机选择一个单词,而不是从文本文件中打印所有内容。”。。如果你想让你的编译器做到这一点,那么你需要自己编写一个。
TextFile file1 = new TextFile ();

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