我目前正在尝试计算文本文件中有多少单词具有偶数和奇数字符但我似乎无法使其工作。到目前为止我已经完成了
int countEven = 0;
int countOdd = 0;
for (int i = 0; i <latinLength.length(); i++) {
if (Character.isLetter(latinLength.charAt(i))) {
countEven++;
} else {
countOdd++;
}
}
System.out.println("Total number of unique even words in Latin names = " + countEven);
System.out.println("Total number of unique odd words in Latin names = " + countOdd);
}
我认为我做错了是我没有访问文本文件的正确部分。我有一个get函数,我想要的信息是getLatinName,但我不知道如何正确实现它
String tempLatinName = " ";
String latinLength = " ";
int letters = 0;
for (int i = 0; i < info.size(); i++) {
tempLatinName = info.get(i).getLatinName();
latinLength = tempLatinName.replace(" ","");
letters += latinLength.length();
}
System.out.println("Total number of letters in all Latin names = " + letters);
我编辑了代码以显示我在尝试计算有多少个单词有奇数和偶数个字符之前所做的位,上面的代码是计算每个单词中的字符总数然后给我一个总数
/**
*
* @author g_ama
*/
import java.io.*;
import java.util.*;
public class Task1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader reader = new BufferedReader(new FileReader("shark-data.txt"));
String line;
List<Shark> info = new ArrayList<>();
while ((line = reader.readLine()) != null) {
String[] data = line.split(":");
int MaxLength = Integer.parseInt(data[2]);
int MaxDepth = Integer.parseInt(data[3]);
int MaxYoung;
try {
MaxYoung = Integer.parseInt(data[4]);
} catch (Exception X) {
MaxYoung = -1;
}
int GlobalPresence = Integer.parseInt(data[5]);
ArrayList<String> OceanicRegion = new ArrayList<>();
String[] Region = data[6].split(",");
for (String Element : Region) {
OceanicRegion.add(Element);
}
Shark shark = new Shark(data[0], data[1], MaxLength, MaxDepth, MaxYoung, GlobalPresence, OceanicRegion);
info.add(shark);
}
Collections.sort(info);
System.out.println("The three largest sharks");
System.out.println(info.get(info.size() - 1).getCommonName() + ", " + info.get(info.size() - 1).MaxLength + " cm");
System.out.println(info.get(info.size() - 2).getCommonName() + ", " + info.get(info.size() - 2).MaxLength + " cm");
System.out.println(info.get(info.size() - 3).getCommonName() + ", " + info.get(info.size() - 3).MaxLength + " cm");
System.out.println("The three smallest sharks");
System.out.println(info.get(0).getCommonName() + ", " + info.get(0).MaxLength + " cm");
System.out.println(info.get(1).getCommonName() + ", " + info.get(1).MaxLength + " cm");
System.out.println(info.get(2).getCommonName() + ", " + info.get(2).MaxLength + " cm");
//count total characters for Latin Name
String tempLatinName = " ";
String latinLength = " ";
int letters = 0;
for (int i = 0; i < info.size(); i++) {
tempLatinName = info.get(i).getLatinName();
latinLength = tempLatinName.replace(" ", "");
letters += latinLength.length();
}
System.out.println("Total number of letters in all Latin names = " + letters);
//count even or odd words
int countEven = 0;
int countOdd = 0;
for (int i = 0; i < latinLength.length(); i++) {
if (Character.isLetter(latinLength.charAt(i))) {
countEven++;
} else {
countOdd++;
}
}
System.out.println("Total number of unique even words in Latin names = " + countEven);
System.out.println("Total number of unique odd words in Latin names = " + countOdd);
}
}
答案 0 :(得分:3)
目前,您只计算文本中有多少字母和非字母。这当然不是偶数词或奇数词的数量。
例如,如果你有一个像
这样的词test12foo!$bar
您的代码目前将输出
countEven => 10 // Amount of letters (testfoobar)
countOdd => 4 // Amount of non-letters (12!$)
将此与 if-condition :
进行比较if (Character.isLetter(latinLength.charAt(i))) {
countEven++;
} else {
countOdd++;
}
你想要的是计算你的单词的长度是偶数还是奇数,所以假设像
这样的单词test // length 4, even
foo // length 3, odd
bartest // length 7, odd
然后你想要
countEven => 1 // (test)
countOdd => 2 // (foo, bartest)
相反,您需要将分割您的文字转换为字(tokenize)。之后,您需要为每个单词计算字符数量。如果这是偶数,您可以将countEven
增加一个。同样地countOdd++
如果它是奇数。
核心就是这个条件
word.length() % 2 == 0
如果单词的长度偶数,则为true
,如果单词的长度为 false ,则为%
。您可以自行轻松验证(0
返回除法后的余数,在这种情况下为1
或whitespace
)。
假设您的文字结构很简单,字词总是由test foo bar John Doe
分隔,例如
Path path = Paths.get("myFile.txt");
AtomicInteger countEven = new AtomicInteger(0);
AtomicInteger countOdd = new AtomicInteger(0);
Pattern wordPattern = Pattern.compile(" ");
Files.lines(path) // Stream<String> lines
.flatMap(wordPattern::splitAsStream) // Stream<String> words
.mapToInt(String::length) // IntStream length
.forEach(length -> {
if (length % 2 == 0) {
countEven.getAndIncrement();
} else {
countOdd.getAndIncrement();
}
});
System.out.println("Even words: " + countEven.get());
System.out.println("Odd words: " + countOdd.get());
总而言之,您的所有代码都可能看起来像
Stream
或者没有所有Path path = Paths.get("myFile.txt");
List<String> lines = Files.readAllLines(path);
List<String> words = new ArrayList<>();
// Read words
for (String line : lines) {
String[] wordsOfLine = line.split(" ");
words.addAll(Arrays.asList(wordsOfLine));
}
// Count even and odd words
int countEven = 0;
int countOdd = 0;
for (String word : words) {
if (word.length() % 2 == 0) {
countEven++;
} else {
countOdd++;
}
}
System.out.println("Even words: " + countEven);
System.out.println("Odd words: " + countOdd);
内容:
info
由于您刚刚添加了特定代码,我将添加适合其的解决方案。
在您的代码中,列表Shark
包含所有Shark#getLatinName
个。从那些鲨鱼中,你想要考虑的词由List<String> words = info.stream() // Stream<Shark> sharks
.map(Shark::getLatinName) // Stream<String> names
.collect(Collectors.toList());
表示。所以你需要做的只是某种:
words
并且您可以完全按照其他代码示例中所示使用此Stream
。或者,您不需要将所有内容收集到新列表中,您可以直接留在AtomicInteger countEven = new AtomicInteger(0);
AtomicInteger countOdd = new AtomicInteger(0);
info.stream() // Stream<Shark> sharks
.map(Shark::getLatinName) // Stream<String> names
.mapToInt(String::length) // IntStream length of names
.forEach(length -> {
if (length % 2 == 0) {
countEven.getAndIncrement();
} else {
countOdd.getAndIncrement();
}
});
System.out.println("Even words: " + countEven);
System.out.println("Odd words: " + countOdd);
并继续之前显示的流方法。总而言之:
//count even or odd words
(substitute here)
将其替换为代码中的该部分:
{{1}}