模拟用户输入

时间:2017-05-22 04:54:44

标签: java input user-input simulate

我想尝试模拟多个用户输入。我有这个代码

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainThing {

    public static void main(String[] args){
        //simulating user input
        String simulatedUserInput = "apple" + System.getProperty("line.separator") +
                                    "pie" + System.getProperty("line.separator");

        InputStream savedStandardInputStream = System.in;   //to reset
        System.setIn(new ByteArrayInputStream(simulatedUserInput.getBytes()));
        //----

        addMsg();

        //reset
        System.setIn(savedStandardInputStream);
    }

    //function to affect
    public static void addMsg()
    {
        String msg1 = inputOutput("Enter message 1");
        String msg2 = inputOutput("Enter message 2");
        System.out.println(msg1 +" " + msg2 );
    }

    //basically a scanner
    private static String inputOutput(String message) {
        System.out.println(message);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String returnString = "";
        try {
            returnString = br.readLine();
        }
        catch (IOException e){
            System.out.println("Error reading in value");
        }
        return returnString;
    }
}

我正在尝试模拟“苹果派”,但是当我运行它时,第二条消息始终为空。 line.separator不适合这个吗?

2 个答案:

答案 0 :(得分:1)

你不应该对BufferedReader两次初始化。第一次初始化得到整个输入,而你的第二次初始化不会有任何进一步的输入,因为Stream已经消耗了你之前“插入”的所有行。好像您的第二个readline会产生null

可以通过这种方式解决错误:

 // Create a static BR which you only initialize once
 static BufferedReader br;

// Inside inputOutput
...
private static String inputOutput(BufferedReader br, String message) {
    ....
    if (br == null)
        br = new BufferedReader(new InputStreamReader(System.in));
    ...
}

或者只是事先初始化BufferedReader并将其传递给方法。

// function to affect
public static void addMsg() {
    BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
    String msg1 = inputOutput(br, "Enter message 1");
    String msg2 = inputOutput(br, "Enter message 2");
    System.out.println(msg1 + " " + msg2);
}
// basically a scanner
private static String inputOutput(BufferedReader br, String message) {
    System.out.println(message);
    String returnString = "";
    try {
        returnString = br.readLine();
    } catch (IOException e) {
        System.out.println("Error reading in value");
    }
    return returnString;
}

编辑: 您还可以将String输入作为数组提供,并为每个数组元素创建ByteArrayInputStream,而不是从单个ByteArrayInputStream创建一个String

public static void main(String[] args){
    //simulating user input as array
    String simulatedUserInput[] = {"apple",
                                   "pie"};

    InputStream savedStandardInputStream = System.in;   //to reset
    //----

    addMsg(simulatedUserInput);

    //reset
    System.setIn(savedStandardInputStream);
}

//function to affect
public static void addMsg(String[] simulatedUserInput)
{
    String msg = "";
    // Loop over the input array and provide the input
    for (int i = 0;i < simulatedUserInput.length; ++i) {
        System.setIn(new ByteArrayInputStream(simulatedUserInput[i].getBytes()));
        msg += inputOutput("Enter message " + (i + 1)) + " ";
    }
    // output the input
    System.out.println(msg);
}

所有情况下的O / P:

Enter message 1
Enter message 2
apple pie

答案 1 :(得分:1)

当您传递System.in时,BufferedReader从流中读取所有字节。你只读苹果而忘记馅饼。第二次BufferedReader看到来自此流的所有字节都已读取。这是你获得null的主要原因。