2015-10-26 60 views
-1

這個程序應該接受信息(通過文件重定向),並應該輸出它給出的內容,包括空格,而不是最後一行。它應該是完全一樣的,沒有任何區別。我的教授說這是錯誤的。這個應該輸出確切輸入的java程序有什麼問題?

import java.io.*; 
public class driver_proj0{ 
public static void main(String[] args){ 
    BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); 
    String lineInput = ""; 
    try { 
     lineInput = f.readLine(); 
    } 
    catch (IOException e){ 
     e.printStackTrace(); 
    } 
    while (lineInput != null){ 
      System.out.print(lineInput); // I just want to print it without a new line out for now 
     try { 
      lineInput = f.readLine(); 
     } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } 
     if (lineInput != null){ 
      System.out.println(); 
     } 
    } // end while 
} //end main 
}// end class 
+0

你必須使用基於行的緩衝嗎?爲什麼你不能只是在許多字節中啜泣,並再次吐出它們呢?你最後'lineinput!= null' println可能是什麼東西在扔掉。 –

+0

什麼是錯的?你得到什麼輸入和輸出? – yogidilip

+1

你問過你的教授嗎? – lrnzcig

回答

0

包括空格,並在年底不是一個多餘的」。如果這是要求,那麼你不應該評論/刪除線

if (lineInput != null){ 
     System.out.println(); 
    } 

這是引入newLine。您的程序正在返回與您的輸入相同的輸出,但是行格式化是某種不被期望的事情。

0
package eu.duernau.stackoverflow; 

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

public class Sov33354885 { 

    public static void main(String[] a) throws IOException { 
     BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); 
     String lineInput = f.readLine(); 
     while (lineInput != null) { 
      System.out.println(lineInput); 
      lineInput = f.readLine(); 
     } 
    } 
} 

產生,例如:

say hello with blanks 
say hello with blanks 
say anything else 
say anything else 
test 
test 
exit does not work, because not implemented.   three tabs 
exit does not work, because not implemented.   three tabs 

你不能得到

say anything elsesayanything else 

,因爲在你輸入斷行了。否則,readLine()將不起作用。 Javadoc for readLine():

讀取一行文本。換行符被換行符('\ n'),回車符('\ r')或回車符後面的換行符中的任何一個結束。