2013-08-17 36 views
2

我試圖從用戶「輸入」文件中計數行,字,字符。
之後顯示計數並再次詢問。使用提示和用戶輸入的掃描儀

如果文件不存在,則打印所有在運行過程中計算的數據。

代碼:

public class KeepAskingApp { 
    private static int lines; 
    private static int words; 
    private static int chars; 

public static void main(String[] args) { 
    boolean done = false; 
    //counters 
    int charsCount = 0, wordsCount = 0, linesCount = 0; 
    Scanner in = null; 
    Scanner scanner = null; 
    while (!done) { 
     try { 
      in = new Scanner(System.in); 
      System.out.print("Enter a (next) file name: "); 
      String input = in.nextLine(); 

      scanner = new Scanner(new File(input));  
      while(scanner.hasNextLine()) { 
       lines += linesCount++; 
       Scanner lineScanner = new Scanner(scanner.nextLine()); 
       lineScanner.useDelimiter(" "); 
       while(lineScanner.hasNext()) { 
        words += wordsCount++; 
        chars += charsCount += lineScanner.next().length(); 
       } 
       System.out.printf("# of chars: %d\n# of words: %d\n# of lines: ", 
         charsCount, wordsCount, charsCount); 

       lineScanner.close(); 
      } 

      scanner.close(); 
      in.close(); 
     } catch (FileNotFoundException e) { 
      System.out.printf("All lines: %d\nAll words: %d\nAll chars: %d\n", 
        lines, words, chars); 
      System.out.println("The end"); 
      done = true; 
     } 
    } 
} 

}

但我不明白爲什麼它總是顯示輸出不帶參數:

All lines: 0 
All words: 0 
All chars: 0 
The end 

爲什麼它忽略了所有內部零件。
這可能是因爲我使用幾個掃描儀,但都看起來不錯。 有什麼建議嗎?

UPDATE:

感謝所有誰給了一些暗示。我重新思考所有構建和重寫代碼與新信息。
要awoid棘手的掃描儀輸入線,我用JFileChooser

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import javax.swing.JFileChooser; 

public class KeepAskingApp { 
    private static int lines; 
    private static int words; 
    private static int chars; 

    public static void main(String[] args) { 
     boolean done = false; 
     // counters 
     int charsCount = 0, wordsCount = 0, linesCount = 0; 
     Scanner in = null; 
     Scanner lineScanner = null; 
     File selectedFile = null; 
     while (!done) { 
      try { 
       try { 
        JFileChooser chooser = new JFileChooser(); 

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
         selectedFile = chooser.getSelectedFile(); 
         in = new Scanner(selectedFile); 
        } 
        while (in.hasNextLine()) { 
         linesCount++; 
         lineScanner = new Scanner(in.nextLine()); 
         lineScanner.useDelimiter(" "); 
         while (lineScanner.hasNext()) { 
          wordsCount++; 
          charsCount += lineScanner.next().length(); 
         } 

        } 
        System.out.printf(
          "# of chars: %d\n# of words: %d\n# of lines: %d\n", 
          charsCount, wordsCount, linesCount); 

        lineScanner.close(); 
        lines += linesCount; 
        words += wordsCount; 
        chars += charsCount; 

        in.close(); 
       } finally { 
        System.out.printf(
          "\nAll lines: %d\nAll words: %d\nAll chars: %d\n", 
          lines, words, chars); 
        System.out.println("The end"); 
        done = true; 
       } 
      } catch (FileNotFoundException e) { 
       System.out.println("Error! File not found."); 
      } 
     } 
    } 
} 
+1

這是不好的代碼,有很多原因,會讓你搞不清楚:'words + = wordsCount ++;'但不是數字是0的原因。你應該在不同的行上使用這段代碼。在該行的所有單詞已被計算後,您應該添加單詞*。 –

回答

2

幾個問題(實際上有與您的代碼中的許多問題,但我會解決直接關係到您已發佈的輸出的):

首先,在catch塊只發生,如果東西你會得到一個FileNotFoundException;那是在那裏處理和從錯誤中恢復。我懷疑你打算在那裏放一個finally區塊,或者你打算在catch之後這樣做。我建議閱讀catching and handling exceptions這個教程,它直截了當地描述了try,catchfinally

一旦你閱讀了教程,回到你的代碼;你可能會發現你有一點改組的可能。

其次,考慮到上述情況,通過輸出結果可以看出,您正在執行catch塊中的代碼,這意味着您得到FileNotFoundException。這可能是由兩個(可能是顯而易見的)之一引起的:

  1. 沒有找到您輸入的文件。它可能不存在,或者它可能不是你期望的地方。檢查以確保您輸入的文件名正確並且文件確實存在。

  2. input字符串不是你所期望的。也許你從前面的輸入中讀出一個空行,等等

尋址理由2:如果已經有上無論什麼原因,輸入緩衝器中的換行,你會讀到一個空行與Scanner。您可能希望在打開文件之前打印input的值,以確保它符合您的期望。

如果您看到空行,請跳過它們。因此,而不是這樣的:

String input = in.nextLine(); 
scanner = new Scanner(new File(input));  

這樣的事情,而不是將免疫空白行:

String input; 
do { 
    input = in.nextLine().trim(); // remove stray leading/trailing whitespace 
} while (input.isEmpty()); // keep asking for input if a blank line is read 
scanner = new Scanner(new File(input)); 

最後,我想你可以工作了,你看到的是0的的原因你的輸出。當您嘗試使用new Scanner(new File(input));打開該文件並且由於無法找到該文件而失敗時,它會拋出異常並且程序立即跳轉到您的catch塊中的代碼。這意味着lines,wordschars的初始值仍爲零(所有修改它們的代碼均被跳過)。

希望有所幫助。

+0

我做了一些重構。用'input'改變了'JFileChooser'這個棘手的時刻,但它一直在拋出'file not found'並關機? [使用JFileChooser更新版本](http://pastebin.com/GDwkYtSr) –

+0

我剛剛嘗試過您的更新版本(現在看起來好多了,文件選擇器是個好主意)。我用文件選擇器選擇了一個文本文件,並打開它並運行得很好。什麼是FileNotFoundException的錯誤信息?此外,你應該在你的問題中發佈你的更新代碼,以便其他可能想要貢獻的人知道它。 :) –

+0

我解決了這個問題,因爲我使用Ubuntu並沒有足夠的權限。但它應該繼續詢問下一個輸入文件,並且只有當您提供錯誤的文件(不存在)時,纔會爲所有反對的數據提供最後部分... –

1

println() s爲在catch

} catch (FileNotFoundException e) { 
     System.out.printf("All lines: %d\nAll words: %d\nAll chars: %d\n", 
       lines, words, chars); 
     System.out.println("The end"); 
     done = true; 
    } 

這意味着你抓了FileNotFoundException。我想你可以從這裏弄明白。