2016-03-03 92 views
1

我無法理解如何在文本文件中計算控制字符。我的程序只會跳過控制字符\n \r: 文件內容:在進一步嘗試後確定我離得更近。如果我改變:如何在文本文件中不計算控制字符

while (input.hasNext()) { 
      String line = input.nextLine(); 
      lineCount++; 
      wordCount += countWords(line); 
      charcount += line.length(); 
to 
while (input.hasNext()) { 
      String line = input.next(); 
      lineCount++; 
      wordCount += countWords(line); 
      charCount += line.replace("\n", "").replace("\r", "").length(); 

字符被計數,但它弄亂了行。如果我添加input.nextLine,它會弄亂字符。文本文件的 內容:

傷心狗
狗搖擺

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

public class Character_count { 

public static void main(String args[]) throws Exception { 

    java.io.File file = new java.io.File("textFile.txt"); 

     // Create a Scanner for the file 
     Scanner input = new Scanner(file); 
     int charcount = 0; 
     int wordCount = 0; 
     int lineCount = 0; 

     while (input.hasNext()) { 
      String line = input.nextLine(); 
      lineCount++; 
      wordCount += countWords(line); 
      charcount += line.length(); 
     } 


     System.out.println("The file " + file + " has "); 
     System.out.println(charcount + " characters"); 
     System.out.println(wordCount + " words"); 
     System.out.println(lineCount + " lines"); 

     } 

    private static int countWords(String s) { 
     Scanner input = new Scanner(s); 
      int count = 0; 

     while (input.hasNext()) { 
      input.next(); 
     count++; 
     } 
     return count; 

    } 
} 
+0

對於字符計數,請在計算它們之前用'line'中的任何內容替換回車符和換行符。類似於'charcount + = line.replace(「\ n」,「」).replace(「\ r」,「」).length()' – Taelsin

+0

這對我不起作用 – jake

回答

1

您可以通過使用useDelimiter方法實現與您Scanner

Scanner input = new Scanner(new File("textFile.txt")); 
input.useDelimiter("\r\n"); 

,並繼續與你的代碼平常,應該工作。

同時,(非常重要)如果你檢查hasNext()然後使用next(),如果你檢查hasNextLine()使用nextLine()!不要混合搭配,因爲它會導致(或已經造成)問題。

+0

這對我不起作用。它仍然計算空間。這是在我的讀數中提到,但非常含糊 – jake

+0

你也在「另外」(使用'hasNextLine()'而不是'hasNext()'') – Idos

+0

是我試過兩種方式 – jake

0

從Scanner.nextLine()的文檔:此方法返回當前行的其餘部分,排除末尾的任何行分隔符。所以你永遠不會計算\ r也不\ n。一個快速解決方案是根據您的(平臺)文本文件格式爲每行添加1或2,但如果文件不是預期的格式,則可能會導致結果不準確。系統屬性「line.separator」會告訴你平臺應該爲行分隔符設置多少(以及哪個)字符。

1

您可以取代所有的\n\r空字符串是這樣的:

line = line.replaceAll("\\r?\\n", "") 

現在你可以做的計數,它不會考慮任何\n\r

你可以交替(不使用正則表達式):

line = line.replace("\n", "").replace("\r", "") 
+0

我試過這個沒有運氣。 – jake

+0

@jake顯示一些它不起作用的示例輸入和輸出。 – Atri

+0

現在有些作品了。請參閱我的更改 – jake

1

你好,你應該在代表則表達式中使用 '\ S' 空格

\ s代表「空白字符」。再一次,這實際包含哪些字符取決於正則表達式的風格。在本教程討論的所有風格中,它都包含[\ t \ r \ n \ f]。即:\ s匹配空格,製表符,換行符或換頁符。(http://www.regular-expressions.info/shorthand.html

所以在這裏你如何使用它

Scanner scanner = new Scanner(path.toFile(),"UTF-8"); 
    String content = scanner.useDelimiter("\\A").next(); 
    System.out.println(content); 

    Pattern patternLine = Pattern.compile("\\r?\\n"); 
    Matcher matcherLine = patternLine.matcher(content); 
    int numberLines = 1; 
    while (matcherLine.find()) 
     numberLines++; 


    Pattern pattern = Pattern.compile("\\s"); 
    Matcher matcherEliminateWhiteSpace = pattern.matcher(content); 
    String contentWithoutWhiteSpace=matcherEliminateWhiteSpace.replaceAll(""); 


    // it will count only ASCII Charachter a->z A->Z 0->9 _'underscore' 
    Pattern patternCharachter=Pattern.compile("\\w"); 
    Matcher matcherCharachterAscii= patternCharachter.matcher(contentWithoutWhiteSpace); 


    int numberCharachtersAscii = 0; 
    while (matcherCharachterAscii.find()) 
     numberCharachtersAscii++; 
    //it will count UTF-8 charachters it will count all charachter no matter what script it is like français عربي and punctuation 
Pattern patternUniversal= Pattern.compile("."); 
    Matcher matcherUniversal= patternUniversal.matcher(contentWithoutWhiteSpace); 
    int numberUniversalCharachter=0; 
    while(matcherUniversal.find()) 
    numberUniversalCharachter++; 
    System.out 
      .println("******************************************************"); 
    System.out.println(contentWithoutWhiteSpace); 
    System.out.println(numberLines); 
    System.out.println(numberCharachtersAscii); 
    System.out.println(numberUniversalCharachter); 
  • 編輯

這裏是一個簡單的修改,這將使它的工作

 while (scanner.hasNext()) { 
      String line = scanner.nextLine(); 
      lineCount++; 
      wordCount += countWords(line); 
      charcount += word.replaceAll("\\s", "").length(); 
      System.out.println(charcount); 
      i++; 
    } 

\\代表白色空格[tab cariag返回lineFeed space formFeed]

+0

我認爲這與我正在進行的工作有很大的關係。 – jake

+0

忽略了那部分數UTF-8 charachters和你的設置只是delet的petternUniversal和什麼來與它,你完成 – achabahe

+0

告訴我你想要什麼,我會使它簡單 – achabahe