首页 > 解决方案 > 计数字符、Java 程序和 wc 产生不一致的结果

问题描述

我写了一个java程序来计算文件中的字符数。为了检查程序是否正常工作,我在命令行 (linux) 中输入这个来检查字符数:

wc -m fileName

从手册页中wc,我知道换行符包含在计数中。

这是我的java程序:

import java.io.IOException;
import java.io.File;
import java.util.Scanner;

public class NumOfChars {
  /** The main method. */
  public static void main(String[] args) throws IOException {
    // Check that command is entered correctly
    if (args.length != 1) {
      System.out.println("Usage: java NumOfChars fileName");
    }

    // Check that source file exists
    File file = new File(args[0]);
    if (!file.exists()) {
      System.out.printf("File %s does not exist\n", file);
    }

    // Create Scanner object
    Scanner input = new Scanner(file);

    int characters = 0;
    while (input.hasNext()) {
      
      String line = input.nextLine();

      // The number of characters is the length of the line plus the newline character
      characters += line.length() + 1;
    }
    input.close();

    // Print results
    System.out.printf("File %s has\n", args[0]);
    System.out.printf("%d characters\n", characters);
  }
}

我遇到的问题是,有时使用 java 程序报告的字符数与使用wc命令时得到的字符数不同。

这里有两个例子:

一个有效的。该文件的内容text.txt

This is some text
This is some text
This is some text
This is some text
This is some text
This is some text
This is some text
This is some text

该命令wc -m text.txt告诉我这个文件有144 个字符。这很好,因为当我执行 java 程序java NumOfChars text.txt时,我还被告知该文件有144 个字符。

一个不起作用的。文件内容Exercise06.java

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/** Converts a hexadecimal to a decimal. */
public class Exercise06 {
  /** Main method */
  public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter a string
    System.out.print("Enter a hex number: ");
    String hex = input.nextLine();
    
    // Display result
    System.out.println("The decimal value for hex number "
      + hex + " is " + hexToDecimal(hex.toUpperCase()));
  }
  

  /** Converts hexadecimal to decimal.
      @param hex The hexadecimal
      @return The deciaml value of hex
      @throws NumberFormatException if hex is not a hexadecimal
    */
  public static int hexToDecimal(String hex) throws NumberFormatException {
    // Check if hex is a hexadecimal. Throw Exception if not.
    boolean patternMatch = Pattern.matches("[0-9A-F]+", hex);
    if (!patternMatch) 
      throw new NumberFormatException();

    // Convert hex to a decimal
    int decimalValue = 0;
    for (int i = 0; i < hex.length(); i++) {
      char hexChar = hex.charAt(i);
      decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
    }
    // Return the decimal
    return decimalValue;
  }
  
  
  /** Converts a hexadecimal Char to a deciaml.
      @param ch The hexadecimal Char
      @return The decimal value of ch
    */
  public static int hexCharToDecimal(char ch) {
    if (ch >= 'A' && ch <= 'F')
      return 10 + ch - 'A';
    else // ch is '0', '1', ..., or '9'
      return ch - '0';
  }
}

该命令wc -m Exercise06.java告诉我这个文件有1650 个字符。但是,当我执行 java 程序java NumOfChars Exercise06.java时,我被告知该文件有1596 个字符。

我似乎无法弄清楚我做错了什么。谁能给我一些反馈?

**编辑:这是我输入时得到的head -5 Exercise06.java | od -c 在此处输入图像描述

标签: javalinuxgnu-coreutils

解决方案


有几种可能的解释:

  • 有可能每行以多个字符结尾,例如在 Windows 上,每行以 CR + LF 结尾,而您的程序总是精确计算 1 个行结束字符。

  • wc可能假设与您的程序不同的字符编码,可能导致多字节字符的字符计数不同。


推荐阅读