gpt4 book ai didi

java - 当文件有多个空格时如何计算文件中的单词数? - java

转载 作者:行者123 更新时间:2023-11-30 07:52:15 26 4
gpt4 key购买 nike

我试图在 linux 中实现命令“wc 文件名”的功能。此命令计算以下数量:

  • 单词
  • 字节数

在文件中。

这是我的代码:

public class wc {
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;

Scanner in = null;

try(Scanner scanner = new Scanner(new BufferedReader(new FileReader(new File("Sample.txt"))))){
File file = new File("Sample.txt");

while (scanner.hasNextLine()) {

String tmpStr = scanner.nextLine();
if (!tmpStr.equalsIgnoreCase("")) {
String replaceAll = tmpStr.replaceAll("\\s+", "");
charsCount += replaceAll.length();
wordsCount += tmpStr.split(" ").length;
}
++linesCount;
}

System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
System.out.println("# of bytes: " + file.length());

}
}
}

问题是在文件中有这样的文本:

Hex Description                 Hex Description

20 SPACE
21 EXCLAMATION MARK A1 INVERTED EXCLAMATION MARK
22 QUOTATION MARK A2 CENT SIGN
23 NUMBER SIGN A3 POUND SIGN

有多个不同长度的空间。有时翻倍,有时甚至更多。如何重构我的代码以便能够正确计算字数?如何去掉多个空格?

最佳答案

String#split接受一个正则表达式,所以你可以简单地拆分 \\s+ (多个空格):

public static void main (String[] args) {
String input = "Some input with more than one space";
String[] words = input.split("\\s+");
System.out.println(words.length + " words");
}

输出:

7 words

参见 on ideone.com .

关于java - 当文件有多个空格时如何计算文件中的单词数? - java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46121917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com