2015-09-16 47 views
2

我正在輸入來自輸入(IDE eclipse Luna)的行時按下輸入光標不斷移動下來,但沒有輸出顯示。在嘗試第二種方法,但我不得不按兩次輸入打印輸出我怎樣才能解決這兩個錯誤。在只要檢測空白行第二種方法爲什麼不能打印爲什麼我需要按Enter鍵兩次,當我們按下控制檯上輸入BufferReader讀取空行

第一種方法

private static String para = null; 
public static void main(String[] args) throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(System.in)); 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
     para = para + line; 
    } 
    bufferedReader.close(); 
    System.out.println(para); 
} 

SECOND什麼命令去方法

String line; 
    while (!(line = br.readLine()).equals("")) { 
     s1 = line.split(" "); 
     for (int j = 1; j < s1.length; j++) { 
      int m = Integer.parseInt(s1[j]); 
      edges[Integer.parseInt(s1[0]) - 1].add(vertices[m - 1]); 
     } 

    } 
+0

謝謝,但是,我想從控制檯讀取行將它們轉換爲單行執行一些操作,然後打印結果我不想打印行,我讀,我想把輸入在控制檯時,我按輸入它應該返回單個結果,所以有什麼解決方案在緩衝區閱讀器中? – JSONParser

回答

0

當您按下恩特雷里奧斯鍵bufferedReader對象收到一個空字符串,所以它不是空。

要結束輸入應該尋找一個鍵像這樣:

private static String para = null; 

public static void main(String[] args) throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(System.in)); 
    String line; 
    while (!"end".equals((line = bufferedReader.readLine()))) { 
     para = para + line; 
    } 
    bufferedReader.close(); 
    System.out.println(para); 
} 

這裏我選擇「結束」作爲結束序列,但你可以選擇任何東西,如空字符串至極是按輸入結果在一條空行上。

+0

我正在閱讀一個問題的輸入,我無法改變那裏發生的事情,那就是我無法在自己的末尾添加結尾。現在做什麼? – JSONParser

+0

使用空字符串停止輸入 –

0

沒有輸出顯示爲什麼?

,因爲你不打印內環任何事情

而(!(線= bufferedReader.readLine())。等於( 「」))//讀取直到沒有輸入任何內容,進入壓。

private static String para = ""; // initialize your para with empty string(i.e "") not with null otherwise null will be added to your para string. 
public static void main(String[] args) throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(System.in)); 
    String line=bufferedReader.readLine(); 
    do{ 
     System.out.println(line); // print what you input 
     para = para + line; 
    } 
    while (!(line = bufferedReader.readLine()).equals("")); 
    bufferedReader.close(); 
    System.out.println(para); 
} 
1

方法一不會結束以來,如下的readLine()永遠不會返回null,而從控制檯輸入讀它總是返回空字符串。

while ((line = bufferedReader.readLine()) != null) { para = para + line; }

記住在按下輸入java認爲它爲換行符()命令,這肯定不爲空,所以你的程序就與空字符串,並不會從while循環終止。

我建議有一些意義完全檢查終止while循環。您可以嘗試Scanner而不是BufferedReader,因爲它提供了很多實用方法。

In Scanner you have methods like hasNext(), hasNextLine() etc which tells whether you have further input or not. Similarly methods like nextLine(), nextInt(), nextDouble() are there to get specific converted values from the command line or any other stream.

方法2正在做很多那些沒有在方法1的部分業務,能否請您爲共享相同的全運行的代碼。我懷疑終止循環的邏輯。

所以我會建議使用更具體的檢查來終止你的循環,同時迭代一些數據。

+0

而如果您正在從某個文件讀取數據,則第一個方法仍然有效,因爲一旦完成文件讀取,最後將返回null方法readLine()。 –

+0

謝謝,但是,我想從控制檯讀取線路將它們轉換爲單行做一些操作,然後打印結果我不想打印行我讀,我想在輸入控制檯時,我按下輸入它應該返回那個單獨的結果,所以有什麼解決方案在bufferreader中? – JSONParser

+0

對不起,請您具體說明一下,例如一步一步舉例,我會試着給你一個解決方案。 –