2011-03-05 2 views
2

사용자에게 명령 줄 입력을 요청하고 입력을 stdin에서 String으로 읽고 반환합니다. 처음 호출 할 때 모든 것을 제대로 작동합니다. 이후에 getInput()에 대한 모든 호출은 아무 것도 반환하지 않습니다.Java System.in에서 입력을 읽는 데 문제가 발생합니다.

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; 
    } 
    // .. 
    } 

답변

9

표준 입력 스트림을 닫지 마십시오. 이것이 처음에만 작동하는 이유입니다.

/** 
* 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

자바의'Console' 클래스는 다소 더 안전한 readPassword 메소드를 가지고 있습니다. – Max

관련 문제