Java计算单词和字符
我应该创建一个程序,计算文本文件中的单词数和平均字符数(不带空格)。我已经创建了这个程序我只是有一个问题。我的字符总数计数器计算字符“-”和“.”在计算单词时,我不希望发生这种情况。目前我统计了300个字符。计数器中应该删除四个“.”和一个“-”,这样我就可以得到295的总值。我尝试使用char,但遇到了一些错误,无法将字符串与char进行比较。我的朋友建议我比较我应该尝试合并一种方法来比较字符和Unicode,但我不知道如何开始编码Java计算单词和字符,java,string,text-files,Java,String,Text Files,我应该创建一个程序,计算文本文件中的单词数和平均字符数(不带空格)。我已经创建了这个程序我只是有一个问题。我的字符总数计数器计算字符“-”和“.”在计算单词时,我不希望发生这种情况。目前我统计了300个字符。计数器中应该删除四个“.”和一个“-”,这样我就可以得到295的总值。我尝试使用char,但遇到了一些错误,无法将字符串与char进行比较。我的朋友建议我比较我应该尝试合并一种方法来比较字符和Unicode,但我不知道如何开始编码 public class Count { public s
public class Count {
public static void main(String[] args) {
File sFile = new File("s.txt");
FileReader in;
BufferedReader readFile;
String sourceCode;
int amountOfWords = 0;
int amountOfChars = 0;
try {
in = new FileReader(sFile);
readFile = new BufferedReader(in);
while ((sourceCode = readFile.readLine()) != null) {
String[] words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
}
}
System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));
readFile.close();
in.close();
} catch (FileNotFoundException e) {
System.out.println("File does not exist or could not be found.");
System.err.println("FileNotFoundException: " + e.getMessage());
} catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
}
}
做之前
String[] words = sourceCode.split(" ");
使用replace
e、 g
或者我猜你可以在拼音中使用所有3个字符“,”和“;”。以下是你问题的答案
public static int numberOfWords(File someFile) {
int num=0;
try {
String line=null;
FileReader filereader=new FileReader(someFile);
try (BufferedReader bf = new BufferedReader(filereader)) {
while((line=bf.readLine()) !=null) {
if (!line.equals("\n")) {
if (line.contains(" ") {
String[] l=line.split(" ");
num=num+l.length;
} else {
num++;
}
}
}
}
} catch (IOException e) {}
return num;
}
public static int numberOfChars(File someFile) {
int num=0;
try {
String line=null;
FileReader filereader=new FileReader(someFile);
try (BufferedReader bf = new BufferedReader(filereader)) {
while((line=bf.readLine()) !=null) {
if (!line.equals("\n")) {
if (line.contains(" ") {
String[] l=line.split(" ");
for (String s : l) {
s=replaceAllString(s, "-", "");
s=replaceAllString(s, ".", "");
String[] sp=s.split("");
num=num+sp.length;
}
} else {
line=replaceAllString(line, "-", "");
line=replaceAllString(line, ".", "");
String[] sp=line.split("");
num=num+sp.length;
}
}
}
}
} catch (IOException e) {}
return num;
}
public static String replaceAllString(String s, String c, String d){
String temp = s.replace(c ,d);
return temp;
}
代码当前包含“-”和“.”,因为它们不会被拆分删除。接受这一基线,如何处理现有的“单词中的每个字符”循环以只计算/包括“字母”?为什么排除筛选器对于“、”或“@”仍然有问题?(或类似方法)如何对包含筛选器有用?您还可以签出
字符
类。看看Character.isAlphabetic(int)。您可以使用正则表达式调用split。你知道这些吗?非常感谢你的帮助。
public static int numberOfWords(File someFile) {
int num=0;
try {
String line=null;
FileReader filereader=new FileReader(someFile);
try (BufferedReader bf = new BufferedReader(filereader)) {
while((line=bf.readLine()) !=null) {
if (!line.equals("\n")) {
if (line.contains(" ") {
String[] l=line.split(" ");
num=num+l.length;
} else {
num++;
}
}
}
}
} catch (IOException e) {}
return num;
}
public static int numberOfChars(File someFile) {
int num=0;
try {
String line=null;
FileReader filereader=new FileReader(someFile);
try (BufferedReader bf = new BufferedReader(filereader)) {
while((line=bf.readLine()) !=null) {
if (!line.equals("\n")) {
if (line.contains(" ") {
String[] l=line.split(" ");
for (String s : l) {
s=replaceAllString(s, "-", "");
s=replaceAllString(s, ".", "");
String[] sp=s.split("");
num=num+sp.length;
}
} else {
line=replaceAllString(line, "-", "");
line=replaceAllString(line, ".", "");
String[] sp=line.split("");
num=num+sp.length;
}
}
}
}
} catch (IOException e) {}
return num;
}
public static String replaceAllString(String s, String c, String d){
String temp = s.replace(c ,d);
return temp;
}