2015-01-17 1 views
0

bufferedreader를 사용할 때 행을 건너 뛰고 사용자가 입력 한 내용을 읽습니다. System.out.print();와 같은 콘솔에서 행의 내용을 읽는 방법이 있습니까?java bufferedreader 다른 행 다음에 행을 읽지 않고 읽습니다.

예 : "여기 당신의 나이를 입력 :"

을 (여기 읽기) 대신 : "여기 당신의 나이를 입력 :"

가 '

내가 돈 (여기 읽기) 모든 문제에 대해 반드시 bufferedreader를 사용할 필요가 있습니다. 단지 줄 바꿈이 아닌 줄 바꿈을 읽고 싶습니다.

EDIT : 내가 깔아 놓은 프로그램의 코드입니다. 좋은 예입니다.

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

public class Main { 
    public static void main(String[] args) { 
     int y; 
     int z; 
     int x = 0; 
     String line2 = "empty"; 
     String line = "empty"; 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    BufferedReader br2 = new BufferedReader(
      new InputStreamReader(System.in)); 
    System.out.println("enter 2 numbers of which the first is larger than the second"); 
    try { 
     line = br.readLine(); 
    } 

    catch (Exception e) { 
     e.printStackTrace(); 

    } 
    try { 
     line2 = br2.readLine(); 
    } 

    catch (Exception e) { 
     e.printStackTrace(); 

    } 

    y = Integer.parseInt(line); 
    z = Integer.parseInt(line2); 

    if (y < 9 && y > 5 && z > 5 && z < 9) { 
     if (y > z) { 
      x = (int) ((Math.random() * (y - z)) + z); 
     } 
     if (z > y) { 
      x = (int) ((Math.random() * (z - y)) + y); 
     } 
    }else System.out.println("only numbers between 5 and 9!"); 
    int[] getallen = new int[x]; 
    for (int i = 0; i < x; i++) { 
     getallen[i] = (int) ((Math.random() * (y - z)) + z); 
     System.out.println(getallen[i]); 
    } 

} 

}

출력 :

"enter 2 numbers of which the first is larger than the second 2 5 
only numbers between 5 and 9!" 

변수 이름은 네덜란드에 있지만, 어쨌든 내 질문에 정말 관련이없는 : 내가 원하는

"enter 2 numbers of which the first is larger than the second 
2 
5 
only numbers between 5 and 9!" 

.

+0

그것은 당신이 당신이 사용하는 정확한 코드를 게시 할 수 있다면 우리는 더 도움이 될 수 있습니다 내 시스템 (윈도우 7, JDK 1.8.0 25) – Atuos

+0

에서 원하는 방식으로 작동합니다. –

+0

@ isuru-buddhika 편집 – Arrrow

답변

0

나는 간단한 프로그래밍을 만들었고 System.out.println(...)System.out.print(...)으로 변경하여 Eclipse와 함께 작업했습니다. 하지만 번호를 구분하는 데 문제가 있습니다. Enter number(s): 1 2 31 2 3을 하나의 문자열로 반환합니다. 당신이 두 번째 통화 2에서와 세 번째 호출에서, (예를 들어 Enter number(s): 1 2 3 먼저 전화 1에서 당신을 줄 것이다 번호를 지속적으로 끌어 할 수있는 nextInt() 방법을 가지고 있기 때문에

그러므로 나는 당신이 입력을 읽기위한 Scanner를 사용하는 것이 좋습니다 것입니다 3)

예 :

public static void main(String[] args) { 

    int number = 0; 
    Scanner scanner = new Scanner(System.in); 

    System.out.print("Enter number(s): "); 
    while(scanner.hasNextInt()) { 
     number = scanner.nextInt(); 
     System.out.println("Number was " + number); 
    } 
} 
+0

명심하십시오 :'int' 이외의 다른 것을 입력하면 예외가 생깁니다. – KnutKnutsen