在Java中多次读取标准输入

在Java中多次读取标准输入,java,stdin,Java,Stdin,我已经为此挣扎了一段时间,虽然我认为这将是非常简单的哈哈 一点背景信息:我正在制作一个小程序来自动回答一个网络调查(使用SeleniumWebDriver)。每个调查都是在用户和密码下完成的。我的目标是让我的程序用他们的id(比如ID1:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 我创建了一个方法,从文本文件中获取用户和密码,并将它们保存到字符串列表中[2] //

我已经为此挣扎了一段时间,虽然我认为这将是非常简单的哈哈

一点背景信息:我正在制作一个小程序来自动回答一个网络调查(使用SeleniumWebDriver)。每个调查都是在用户和密码下完成的。我的目标是让我的程序用他们的id(比如ID1:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我创建了一个方法,从文本文件中获取用户和密码,并将它们保存到字符串列表中[2]

    // Just a test list with the name (or id) of each question
    List<String> nombrePreguntas = new LinkedList<String>(Arrays.asList("nombre1", "nombre2", "nombre3", "nombre4"));

    // read users from file -> usuarios[0] = user, usuarios[1] = pass.
    List<String[]> usuarios = leerUsuarios("entrada.txt");

    for (String[] u : usuarios) {
        // print each user and pass
        System.out.println(u[0] + " - " + u[1]);
        // for each user+pass, read the questions and ask for their answer in stdin
        List<Integer> respuestas = leerRespuesta(nombrePreguntas);
        System.out.println(respuestas );
    }
我不知道做我想做的事最好的方法是什么。如果您找不到解决方案,我也希望您能提供一些关于替代方法的建议,以便从标准输入中获得每个用户+通行证的答案

这是从stdin读取的函数:

public static List<Integer> leerRespuesta(List<String> nombrePreguntas) {
    List<Integer> respuestas = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    // Para cada pregunta, habrá una respuesta
    boolean licencia = true;
    for (String np : nombrePreguntas) {
        // just a feature that I must implement
        if(licencia == false && (np.equals("LM2") || np.equals("LM3") || np.equals("LM4"))) {
            continue;
        }

        // Print the name/id of the question
        System.out.print(np + ": ");

        if(in.hasNextLine()) {
            String s = in.nextLine();

            while(!StringUtils.isNumeric(s)) {
                System.out.println("It must be a number.");
                System.out.print(np + ": ");
                if(in.hasNextLine())
                    s = in.nextLine();
            }

            if(np.equals("LM1") && s.equals("1"))
                licencia = false;

            respuestas.add(Integer.parseInt(s));
        }
    }
    in.close();
    return respuestas;
}
公共静态列表leerRespuesta(列表名称){
List respuestas=new ArrayList();
扫描仪输入=新扫描仪(系统输入);
//帕拉卡德·普雷孔塔,哈伯拉·普雷斯塔
布尔licencia=真;
for(字符串np:nombrePreguntas){
//这只是我必须实现的一个特性
如果(Licensia==false&(np.equals(“LM2”)| | np.equals(“LM3”)| | np.equals(“LM4”)){
持续
}
//打印问题的名称/id
系统输出打印(np+“:”);
if(在.hasNextLine()中){
字符串s=in.nextLine();
而(!StringUtils.isNumeric){
System.out.println(“它必须是一个数字”);
系统输出打印(np+“:”);
if(在.hasNextLine()中)
s=in.nextLine();
}
如果(np.equals(“LM1”)和s.equals(“1”))
Licensia=假;
add(Integer.parseInt);
}
}
in.close();
返回响应;
}

谢谢。

您无法多次阅读标准输入。Java不支持此1

您可以做的是读取一次标准输入,并将其全部捕获为
字符串。然后从
String
中重复创建并使用
StringReader
Scanner



1。。。。因为典型的操作系统不支持它。

你是想把调查结果叠加起来吗?我不确定我是否理解你的意思(英语不是我的主要语言)。但我想做的是:我想连续回答很多调查,并将他们的答案保存在列表或文本文件中,这样我就可以执行一个方法,询问用户、通行证和答案列表,并将其应用于我阅读的每个用户+通行证和答案;i、 一个真正的人。Jim在问你为什么要编写一个程序来回答多次调查?哦,因为我已经实际回答了调查,我只需要在web平台上输入结果,手动操作太繁琐了。我的意思是,无论如何我都要手动完成,但我宁愿一次完成,用一个简单的程序,而不是整个网络表单,按OK,登录等等。哦,所以我有点搞砸了。好的,我会试试看,但是你会建议我如何区分答案呢?我的意思是,每次调查有140个问题。。。我如何知道一个完成,另一个启动,并将它们“分配”给不同的用户?谢谢
public static List<Integer> leerRespuesta(List<String> nombrePreguntas) {
    List<Integer> respuestas = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    // Para cada pregunta, habrá una respuesta
    boolean licencia = true;
    for (String np : nombrePreguntas) {
        // just a feature that I must implement
        if(licencia == false && (np.equals("LM2") || np.equals("LM3") || np.equals("LM4"))) {
            continue;
        }

        // Print the name/id of the question
        System.out.print(np + ": ");

        if(in.hasNextLine()) {
            String s = in.nextLine();

            while(!StringUtils.isNumeric(s)) {
                System.out.println("It must be a number.");
                System.out.print(np + ": ");
                if(in.hasNextLine())
                    s = in.nextLine();
            }

            if(np.equals("LM1") && s.equals("1"))
                licencia = false;

            respuestas.add(Integer.parseInt(s));
        }
    }
    in.close();
    return respuestas;
}