Java 随机选择的字符串

Java 随机选择的字符串,java,string,random,Java,String,Random,我有一个文本文件,我正在尝试将每一行转换为ArrayList。然后,我必须从这个text.file中随机抽取一行,并将其显示在new JOptionPane上 我试图在for循环中实现它,但它总是只出现在text.file的第一行。非常感谢,这是我的代码 public void actionPerformed(ActionEvent e) { ArrayList<String> allQuestions = new ArrayList<String>();

我有一个文本文件,我正在尝试将每一行转换为ArrayList。然后,我必须从这个text.file中随机抽取一行,并将其显示在new JOptionPane上

我试图在for循环中实现它,但它总是只出现在text.file的第一行。非常感谢,这是我的代码

public void actionPerformed(ActionEvent e) {

    ArrayList<String> allQuestions = new ArrayList<String>();
    ArrayList<String> allRandomSelectedQuestions = new ArrayList<String>();
    File file = new File("C:/Users/User/Documents/NetBeansProjects/SummerExamProject/src/Questions2.txt");
    int numberOfRandomQuestions = 16;

    try {
        //Read line by line from the file  
        Scanner scan = new Scanner(file);

        while (scan.hasNextLine()) {

            String line = scan.nextLine();

            //  System.out.println(line);
            JOptionPane.showMessageDialog(null, line.replace("/", "\n"));

            scan.close();

            allQuestions.add(line); 
        }
    } 
    catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }

    for(int i = 0; i < numberOfRandomQuestions; i++){
        Random randNum = new Random();

        int randQuestionIndex = randNum.nextInt(numberOfRandomQuestions);   
        String randomQuestion = allQuestions.get(randQuestionIndex); 
        allRandomSelectedQuestions.add(randomQuestion);
    }
}
public void actionPerformed(ActionEvent e){
ArrayList allQuestions=新的ArrayList();
ArrayList allRandomSelectedQuestions=新建ArrayList();
File File=新文件(“C:/Users/User/Documents/NetBeansProjects/SummerExamProject/src/Questions2.txt”);
int numberofrandom问题=16;
试一试{
//从文件中逐行读取
扫描仪扫描=新扫描仪(文件);
while(scan.hasNextLine()){
String line=scan.nextLine();
//系统输出打印项次(行);
JOptionPane.showMessageDialog(null,line.replace(“/”,“\n”);
scan.close();
所有问题。添加(行);
}
} 
捕获(FileNotFoundException ex){
例如printStackTrace();
}
对于(int i=0;i
这一行

scan.close();
while
循环中,因此它在第一次通过循环时读取一行后关闭文件


将其移动到循环后(即闭合支架后)应该可以修复它。

问题在于:
scan.close()。您将在用于读取的同一环路中关闭扫描仪。将其移到循环体之外应该可以解决问题。

在while循环之后调用close方法。发生了什么,你在第一行结束后。因此,while循环仅在一次之后停止

 while (scan.hasNextLine()) {

          String line = scan.nextLine();
          //System.out.println(line);
          JOptionPane.showMessageDialog(null, line.replace("/", "\n"));
          allQuestions.add(line); 
    }
    scan.close();

试试这个。希望能有帮助

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class StackTest {

    public static void main(String[] args) {
        actionPerformed();
    }

    public static void actionPerformed() {

        ArrayList<String> allQuestions = new ArrayList<String>();
        File file = new File("D:/me/test.txt");
        int numberOfRandomQuestions = 10;
        try {
            // Read line by line from the file
            Scanner scan = new Scanner(file);

            while (scan.hasNextLine()) {
                String line = scan.nextLine();
                allQuestions.add(line);
            }
            scan.close();

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < numberOfRandomQuestions; i++) {
            Random randNum = new Random();

            int randQuestionIndex = randNum.nextInt(numberOfRandomQuestions);
            System.out.println();
            String randomQuestion = allQuestions.get(randQuestionIndex);
            JOptionPane.showMessageDialog(null, randomQuestion.replace("/", "\n"));
        }
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Random;
导入java.util.Scanner;
导入javax.swing.JOptionPane;
公共类堆栈测试{
公共静态void main(字符串[]args){
动作执行();
}
已执行的公共静态无效操作(){
ArrayList allQuestions=新的ArrayList();
File File=新文件(“D:/me/test.txt”);
int numberofrandom问题=10;
试一试{
//从文件中逐行读取
扫描仪扫描=新扫描仪(文件);
while(scan.hasNextLine()){
String line=scan.nextLine();
所有问题。添加(行);
}
scan.close();
}捕获(FileNotFoundException ex){
例如printStackTrace();
}
对于(int i=0;i
是的,它可以工作,并给我随机的问题。非常感谢你。但是,当我打开一个新的JOptionPane,然后尝试关闭它时,它会连续显示text.file中的所有其他问题,当它们到达text.file的末尾时,它们就会关闭!很抱歉,你没有明白你面临的问题,你能详细说明一下吗?你想一次只打开一个对话框吗?是的,我有16个按钮可以做同样的事情。当我按下其中一个按钮时,它会打开一个JOptionPane,其中包含来自text.file的随机问题,但是,当我单击此JOptionPane上的“确定”或“关闭”时,扫描似乎没有停止,它会在新的JOptionPane-s中连续显示此文件中的每一个问题。您是否希望在破坏程序一次的同时只显示一个问题?是的,当我单击我的JButton显示一个问题,然后回答它时,之后,我可以从16个按钮中选择另一个按钮并加载另一个问题。