gpt4 book ai didi

java - 使用 Scanner 类解析文本文件并通过多个循环计数模式出现次数

转载 作者:行者123 更新时间:2023-12-02 04:48:44 24 4
gpt4 key购买 nike

我在扫描仪上使用多个 while 循环,我有两个 while 循环,其中一个在另一个之前,它将计算出正确的结果,但是下面的 while 循环将被破坏。

我尝试过添加中断;两者都有,但这没有帮助。我究竟做错了什么 ?

 public static void main(String args[]) throws IOException {

Scanner sc = new Scanner(Coursework.class.getResourceAsStream("test.txt")).useDelimiter(("[^A-Za-z']+"));

int count = 0;
int numberOfLines = 0;
int numberOfConsonants = 0;
int numberOfPunctuation = 0;
double average = 0;
int numberOfWords = 0;
int numberOfChars = 0;

String x = "";
String nextLine = "";

while (sc.hasNextLine()) {
nextLine = sc.nextLine();
numberOfLines++;
x = nextLine.toLowerCase().trim();
for (int i = 0, n = x.length(); i < n; i++) {
char c = x.charAt(i);
numberOfChars++;
if ((c == 'b' || c == 'c' || c == 'd' ||
c == 'f' || c == 'g' || c == 'h' ||
c == 'j' || c == 'k' || c == 'l' ||
c == 'm' || c == 'n' || c == 'p' ||
c == 'q' || c == 'r' || c == 's' ||
c == 't' || c == 'v' || c == 'w' ||
c == 'x' || c == 'y' || c == 'z')) {
numberOfConsonants++;

}
if ((c =='?'| c == '.' || c == ',' || c == '"')){
numberOfPunctuation++;
}
}
}

while (sc.hasNext()) {
sc.next();
count++;
}
}

最佳答案

我不确定您的作业需要什么,或者它重点让您学习 Java 语言的哪一部分,但如果我想类似地解析和统计输入文件,我可能会执行如下操作:

其中 file.txt 包含:

1. Aaaaa eiou foo!
2. bbbbb?

程序编译并运行如下:

javac TextAnalyzer.java
java TextAnalyzer file.txt

输出为:

Text Analysis of file: file.txt

characters: 27
words: 6
uppercase: 1
lowercase: 16
consonants: 7
vowels: 11
digits: 2
punctuation: 4
whitespace: 4

这是源代码:

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class TextAnalyzer {

public static int count(String line, Pattern pattern) {
int count = 0;
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
++count;
}
return count;
}

public static void main(String args[]) throws IOException {

Pattern vowels = Pattern.compile("[aeiouAEIOU]");
Pattern consonants = Pattern.compile("[bcdfghjklmnpqrstuvwxyzBCDFGHJKLMNOPQRSTUVWXYZ]");
Pattern punctuation = Pattern.compile("\\p{Punct}");
Pattern whitespace = Pattern.compile("\\p{Space}");
Pattern digits = Pattern.compile("\\p{Digit}");
Pattern uppercase = Pattern.compile("\\p{Upper}");
Pattern lowercase = Pattern.compile("\\p{Lower}");
Pattern words = Pattern.compile("\\w+");
Pattern characters = Pattern.compile(".");

int vowelCount = 0;
int consonantCount = 0;
int punctuationCount = 0;
int whitespaceCount = 0;
int digitCount = 0;
int uppercaseCount = 0;
int lowercaseCount = 0;
int wordCount = 0;
int charCount = 0;
int lineCount = 0;

if (args[0].length() == 0) {
System.out.println("Error: No filename provided");
System.exit(-1);
}

try {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
for (String line; (line = br.readLine()) != null; ) {
++lineCount;
vowelCount += count(line, vowels);
consonantCount += count(line, consonants);
punctuationCount += count(line, punctuation);
whitespaceCount += count(line, whitespace);
digitCount += count(line, digits);
uppercaseCount += count(line, uppercase);
lowercaseCount += count(line, lowercase);
wordCount += count(line, words);
charCount += count(line, characters);
}
} catch (Exception e) {
System.out.println("Couldn't parse " + args[0] + "\n" + e.getMessage());
System.exit(-1);
}

System.out.println("Text Analysis of file: " + args[0]);
System.out.println("");
System.out.println(" characters: " + charCount);
System.out.println(" words: " + wordCount);
System.out.println(" uppercase: " + uppercaseCount);
System.out.println(" lowercase: " + lowercaseCount);
System.out.println(" consonants: " + consonantCount);
System.out.println(" vowels: " + vowelCount);
System.out.println(" digits: " + digitCount);
System.out.println(" punctuation: " + punctuationCount);
System.out.println(" whitespace: " + whitespaceCount);
}
}

关于java - 使用 Scanner 类解析文本文件并通过多个循环计数模式出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29447521/

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