2011-09-28 32 views
0

Soo我的程序正在工作......但只有一個問題...... null也隨着內容或字符串被打印從文件中...無論如何,我可以阻止null被打印。讀取的代碼要求用戶提供文件名.txt,然後向用戶詢問他/她想要搜索的字符,程序在字符串中搜索指定的字符,打印出文本文件的內容並且告訴它被發現了多少次。問題是打印出文本文件(字符串)的內容後,它也會打印出null字樣,但null不在txt文件中。JAVA;當文件中的字符串被打印在屏幕上時null也被打印在一起

import java.util.Scanner; 
import java.io.*; 

public class filefinder 
{ 
    public static void main(String[] args) throws IOException 
    { 


     //needed for scanner class 
      Scanner kb = new Scanner(System.in); 

      int charCount = 0; 
      String filename = ""; 
      String str = ""; 
      String line = ""; 
      boolean isString = false; 
      boolean fileFound = false; 
      FileReader freader; 

      // get users string 
      while (!fileFound) 
      { 
      while (!isString) 
      { 
      try { Thread.currentThread().sleep(500); } 
       catch (Exception e) { } 

      System.out.println("Please enter a filename: "); 
      System.out.println(""); 
      filename = kb.nextLine(); 
      if (filename != null) 
        isString = true; 
      }//end inner while loop 

      // open file 
      try 
      { 
       freader = new FileReader(filename);  

      fileFound = true; 


      Thread.currentThread().sleep(1500); 
      } 
      catch (Exception e) 
      { 

      System.out.println(""); 
      System.out.println("The system cannot find the file specified."); 
      System.out.println(""); 
      isString = false; 


      }//end try-catch 
      }//end outer while loop 

      try { Thread.currentThread().sleep(1000); } 
      catch (Exception e) { } 

      freader = new FileReader(filename); 
      BufferedReader inputFile = new BufferedReader(freader); 

      // Read first line from file 
      while (line !=null) 
      { 

      line = inputFile.readLine(); 

      if (line != null) 


      try { Thread.currentThread().sleep(1000); } 
      catch (Exception e) { } 

       str = str+ "\n" + line; 
      }//end while 

      System.out.println(str); 

      inputFile.close(); 

      // get users character 
      System.out.println(""); 
      System.out.println("Please enter a character you want to find: "); 
      System.out.println(""); 
      char userChar = kb.nextLine().charAt(0); 






        while(str.length()>0){ 
        for(int i= 0;i<str.length();i++){ 
           if(str.charAt(i)==userChar) 
         charCount++; 

       } 


      System.out.println("\n The entered character " +"\"" + userChar + 
           "\" inside " + filename + 
           "was found " + charCount + " times.\n");   
             break; 

     } 
    } 
    } 

回答

1

基本回路

String str = ""; 
while (line != null) { 
    line = inputFile.readLine(); 
    str = str + "\n" + line; 
}// end while 

System.out.println(str); 

這將讀取所有行,直到最後一個。但是由於在while-criterion內檢查了文件結尾(即line==null),因此null行也會附加到str變量中。

+0

,所以我應該從同時crietrtion去除呢? – LOKI

+0

你可以像喬恩在他的回答中提出的那樣去做。 – Howard

4

這就是問題所在:

while (line !=null) 
    { 

    line = inputFile.readLine(); 

    if (line != null) 


    try { Thread.currentThread().sleep(1000); } 
catch (Exception e) { } 

     str = str+ "\n" + line; 
    } 

很難確切說出你認爲這意味着因爲縮進是如此之差,但它是相同的:

while (line !=null) 
{ 
    line = inputFile.readLine(); 
    if (line != null) 
    { 
     try { Thread.currentThread().sleep(1000); } 
     catch (Exception e) { } 
    } 
    str = str+ "\n" + line; 
} 

換句話說,你仍然在循環中連接line,不管它是否爲空;你只是不是睡覺如果它是空的。我懷疑你它是這樣的:

while (line !=null) 
{ 
    line = inputFile.readLine(); 
    if (line != null) 
    { 
     try { Thread.currentThread().sleep(1000); } 
     catch (Exception e) { } 
     str = str+ "\n" + line; 
    } 
} 

強烈建議:

  • 你得到你的IDE爲你,讓你可以清楚看看有什麼所涵蓋執行縮進一個if塊等
  • 總是使用大括號while/if/etc,再次使事情更清晰
  • 你沒有趕上裸Exception(趕上一個更具體的一個)
  • 你不例外,而無需登錄他們
  • 你沒有一秒鐘在一個時間無特殊原因
  • 睡覺
  • 你不會在一個循環中使用字符串連接(use StringBuffer/StringBuilder instead
  • 你打破你的代碼更小的方法
  • 您在第一次使用點聲明局部變量,而不是所有的方法
  • 頂部
  • 您關閉流和finally塊其他類似的資源,以避免在出現異常的情況下,泄漏的資源被拋出
+0

ahh ok即將試用 – LOKI

+0

ahhh好,所以我的代碼風格阻止我進行正確的分析,非常感謝你..我必須現在縮進..我正在使用Jgrasp – LOKI

+0

我認爲你的建議是好的;然而,請考慮使用「You」這個詞,因爲它可能沒有像預期的那樣被接收。 – losthorse