开发者

Issue reading input from Java System.in

I am trying to write a method that prompts a user for input on the command-line and reads their input as a String from stdin and returns in. The first time I call it, everything works properly. All calls to getInput() afterwards just return nothing.

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

/**
 * Prompts the user for input and reads from standard input (stdin).
 * Note: Always check if the return is null!
 * 
 * @param description Describes the user input.
 * @return A String of the input, or null when failed.
 */
private String getInput(String description)
{       
    System.out.print(description + ": 开发者_如何学运维");
    String input = null;

    InputStreamReader stream = null;
    BufferedReader reader = null;
    try {
        // Open a stream to stdin
        stream = new InputStreamReader(System.in);

        // Create a buffered reader to stdin
        reader = new BufferedReader(stream);

        // Try to read the string
        input = reader.readLine();

        // Exhaust remainder of buffer
        while (reader.skip(1) > 0) {
            // Do nothing
        }

    } catch (IOException e) {
        e.printStackTrace();
        // Error reading input

    } finally {
        // Clean up readers and streams
        try {
            if (reader != null) {
                reader.close();
            }
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
        }
    }

    System.out.print("\n");
    return input;
}

/**
 * Display the login prompt.
 */
private boolean promptLogin()
{       
    // prompt for user name and password
    String user = getInput("Enter username");
    String pass = getInput("Enter password");

    if (user == null || pass == null) {
        System.out.println("Invalid login information.");
        return false;
    }
    // ..
     }


You must not close the Standard Input Stream; this is the reason it works only the first time

/**
 * Prompts the user for input and reads from standard input (stdin).
 * Note: Always check if the return is null!
 * 
 * @param description Describes the user input.
 * @return A String of the input, or null when failed.
 */
private String getInput(String description) {
    System.out.print(description + ": ");
    String input = null;

    InputStreamReader stream = null;
    BufferedReader reader = null;
    try {
        // Open a stream to stdin
        stream = new InputStreamReader(System.in);

        // Create a buffered reader to stdin
        reader = new BufferedReader(stream);

        // Try to read the string
        input = reader.readLine();           
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return input;
}

/**
 * Display the login prompt.
 */
private boolean promptLogin() {
    // prompt for user name and password
    String user = getInput("Enter username");
    String pass = getInput("Enter password");

    if (user == null || pass == null) {
        System.out.println("Invalid login information.");
        return false;
    }

    return true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜