2016-06-18 2 views
0

내 프로그램에서는 콘솔에 사용자 이름과 나이를 한 번에 입력해야합니다. 나는 그림과 같이 BufferedReader를 사용하고있다. 보시다시피, addPassenger는 이름과 나이라는 두 가지 입력을 받지만 이름 만 지정할 수 있습니다. BufferedReader를 사용하여 어떻게 구현할 수 있습니까? 즉, 어떻게 "screenInput.readLine();"을 만들 수 있습니까? 이름과 나이를 모두 입력 문자열로 사용합니까? 어떤 도움을 주셔서 감사합니다.BufferedReader가 여러 입력을 한 번에 처리하도록하려면 어떻게해야합니까?

public class Console { 

public static void main(String[] args) { 

    // Initialize database 

    Database prodDB = new Database(); 
    prodDB.bootstrap(); 

    //Initialize console 
    boolean always = true; 
    BufferedReader screenInput = new BufferedReader(new InputStreamReader(System.in)); 

    while(always){ 

     //ask for passengerName and age, then add 
     System.out.println("Enter Passenger Name and Age: "); 

     String name = screenInput.readLine(); 

     boolean result = prodDB.addPassenger(name, age); 

     if (result){ 
      System.out.println("Welcome back " + name); 
     } else 
     { 
      System.out.println("Welcome " + name); 
     } 

     always = false; 
그냥 같이, 나이를 가지고 다른 회선을 추가 할 수 있습니다
+0

는 어떻게 구분합니까? 공간? 들어가다? – Hackerdarshi

+0

나는 그들을 Enter로 구분합니다. –

답변

1

:

String name = screenInput.readLine(); 
int age = Integer.parseInt(screenInput.readLine()); 
+0

감사합니다. 우주와 분리하면 어떻게 될까요? –

+1

@RobertMutua 줄을 읽고 문자열을 분할 ("")하십시오. – theVoid

+0

@RobertMutua theVoid가 맞습니다! 당신은'split ("")'을 사용하고 나이 문자열을 int로 변환 할 수 있습니다. – Hackerdarshi

관련 문제