如何解决字符计数,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
命令时得到的字符数不同。
这里有两个例子:
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 个字符。
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
时得到的信息
解决方法
有几种可能的解释:
-
每行可能以多个字符结尾,例如在Windows上,每行以CR + LF结尾,而您的程序始终只计入1个行结束字符。
-
wc
可能采用与程序不同的字符编码,可能导致多字节字符的字符计数不同。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。