如何统计每行单词中出现的字母,单词中出现A..Z的次数

  • 本文关键字:单词中 何统计 统计 java
  • 更新时间 :
  • 英文 :


我正在通过练习一些基本程序来学习JAVA。我有一个名为samplmain .java的类文件,我在其中每行读取一个文本文件,并将其存储在一个名为fileWords的变量中。

samplmain .java-

中的代码
public static void readFileData() {

Scanner fileInput = null;
try {
fileInput = new Scanner(new File("data.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(fileInput.hasNextLine()) {
String fileWords = fileInput.nextLine();
System.out.println(fileWords);
}
}

输出如下所示的data.txt值-

Swift
Dotnet
Java
Typescript
Python

我有另一个类文件Sample1.java,我想做下面的事情。

  1. 计算字符串(fileWords)中单词的总字母数,并将其存储在另一个名为total的变量中,我还想检查文件不应该有任何垃圾值。

  2. 使用fileWords中单词的大写版本,它应该计算A, B,…

我已经写了我的代码如下-Sample1.java

public class Sample {
public void countLetters() {
int count = 0;
for (int i = 0; i < SampleMain.wordlist.size(); i++) {
if (Character.isLetter(SampleMain.fileWords.get(i).charAt(i)- 25) >= 0 && Character.isLetter(SampleMain.fileWords.get(i) - 25) <= 25) // I'm doing it wrong
SampleMain.total = count++;
}
}

有人能在这里指导我,我在迭代时做错了什么,以及我如何纠正这一点以获得上述计数。

tl;使用代码点整数,而不是过时的char类型。

"Swift🦂"                         // SCORPION character is not a letter.
.codePoints()                     // Generate a stream of `int` primitive values, one for each character in our input string. Represents the code point number assigned to this character by the Unicode Consortium.
.filter( Character :: isLetter )  // 
.count()

详细信息

char已过时

char类型在Java中是过时的,甚至不能表示Unicode中定义的一半字符。学习使用码点整数代替。

码点流

对于字符串中每个字符的代码点,您可以获得int原语值流和IntStream

IntStream codePointsStream = "Swift🦂".codePoints() ;

显然你想把重点放在字母上,而不是数字、标点符号等。因此,通过测试该字符是否为Unicode定义中的字母来进行筛选。

long countLetters = codePointsStream.filter( codePoint -> Character.isLetter( codePoint ) ).count();

我们可以使用方法引用来缩短代码。

long countLetters = codePointsStream.filter( Character :: isLetter ).count();

把这些代码放到一起。

String input = "Swift🦂";
IntStream codePointsStream = input.codePoints();
long countLetters = codePointsStream.filter( Character :: isLetter ).count();
System.out.println( "input = " + input + " | countLetters: " + countLetters );

input = Swift🦂| countLetters: 5

使用常规Java语法

如果您还不熟悉lambdas、streams和方法引用,我们可以通过将代码点整数的IntStream转换为List来获得与传统代码相同的效果。

boxed()的调用在一个称为"装箱"的过程中将int的原语值转换为Integer对象。

List< Integer > codePoints = "Swift🦂".codePoints().boxed().toList() ;  // Before Java 16, replace that `.toList()` with `.collect(Collectors.toList())`. 

循环列表,检查每个元素。如果它通过了作为字母的测试,则增加您的计数器。

int countLetters = 0;
for ( Integer codePoint : codePoints ) {
if ( Character.isLetter( codePoint ) ) { countLetters++; }
}

我正在使用正则表达式来解决您的问题,示例代码如下:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
public static void main(String []args){
int total=0;
int upperCaseCount=0;
Pattern letter_pattren = Pattern.compile("[a-zA-Z]");
Pattern upperCase_pattern=Pattern.compile("[A-Z]");
String data="SwiftnDotnetnJavanTypescriptnPython";
String[] lines =data.split("n");
for (String line : lines){
Matcher letterMatcher = letter_pattren.matcher(line);
while (letterMatcher.find()) {
total++;
}
letterMatcher = upperCase_pattern.matcher(line);
while (letterMatcher.find()) {
upperCaseCount++;
}
}
System.out.println("Total letter count:"+total);
System.out.println("Upper Case letter Count:"+upperCaseCount);
}
}

你可以参考这个链接了解详情。

最新更新