你不计算第一个单词。因此,如果字符串不为空,您必须从 1 开始计数器。
我用这个:
String myFile = "one!!!!! two!!!!! three: Baba? Oho! Bubu and bebe.";
String pattern ="[.|:|;|?|!|,|'| ]";
int counter = 0;
for(String word : myFile.split(pattern)) {
if(word.length()!=0)counter++;
}
System.out.println("Words: "+counter); //print Words: 8
您可以将计数器编辑为:
public static int yourCounter(String myFile) {
if(myFile.length()==0)return 0;
String WORD_DELIMETERS = ".:;?!,' ]";
int wordCounter= WORD_DELIMETERS.contains(myFile.charAt(0)+"")?0:1;
int last_Index=0;
for(int i=0;i<myFile.length()-1;i++){
for(int j=0;j<WORD_DELIMETERS.length()-1;j++){
if(myFile.charAt(i)==WORD_DELIMETERS.charAt(j)){
if(myFile.charAt(i+1) !=' '){
if(last_Index!=i-1){
wordCounter++;
}
last_Index=i;
}
}
}
}
return wordCounter;
}
我使用拆分和正则表达式。看下面:长文本示例:
public static void main(String[] args) throws Throwable {
String text = "Java is a computer programming langua"
+ "ge that is concurrent, class-based, objec"
+ "t-oriented, and specifically designed to "
+ "have as few implementation dependencies a"
+ "s possible. It is intended to let applica"
+ "tion developers \"write once, run anywher"
+ "e\" (WORA), meaning that code that runs o"
+ "n one platform does not need to be recomp"
+ "iled to run on another. Java applications"
+ " are typically compiled to bytecode (clas"
+ "s file) that can run on any Java virtual "
+ "machine (JVM) regardless of computer arch"
+ "itecture. Java is, as of 2012, one of the"
+ " most popular programming languages in us"
+ "e, particularly for client-server web app"
+ "lications, with a reported 9 million deve"
+ "lopers.[10][11] Java was originally devel"
+ "oped by James Gosling at Sun Microsystems"
+ " (which has since merged into Oracle Corp"
+ "oration) and released in 1995 as a core c"
+ "omponent of Sun Microsystems' Java platfo"
+ "rm. The language derives much of its synt"
+ "ax from C and C++, but it has fewer low-l"
+ "evel facilities than either of them. The "
+ "original and reference implementation Jav"
+ "a compilers, virtual machines, and class "
+ "libraries were developed by Sun from 1991"
+ " and first released in 1995. As of May 20"
+ "07, in compliance with the specifications"
+ " of the Java Community Process, Sun relic"
+ "ensed most of its Java technologies under"
+ " the GNU General Public License. Others h"
+ "ave also developed alternative implementa"
+ "tions of these Sun technologies, such as "
+ "the GNU Compiler for Java (bytecode compi"
+ "ler), GNU Classpath (standard libraries),"
+ " and IcedTea-Web (browser plugin for appl"
+ "ets).";
System.out.println("Text:\n"+text+"\n--------------------\nWords: "+countWords(text) + "\nSentecens: " + countSentecens(text)
+ "\nVowels: " + countVowels(text) + "\nChars: "
+ text.toCharArray().length + "\nUpper cases: "
+ countUpperCases(text)+"\nYour counter of words: "+yourCounter(text));
}
public static int yourCounter(String myFile) {
if(myFile.length()==0)return 0;
String WORD_DELIMETERS = ".:;?!,' ]";
int wordCounter= WORD_DELIMETERS.contains(myFile.charAt(0)+"")?0:1;
int last_Index=0;
for(int i=0;i<myFile.length()-1;i++){
for(int j=0;j<WORD_DELIMETERS.length()-1;j++){
if(myFile.charAt(i)==WORD_DELIMETERS.charAt(j)){
if(myFile.charAt(i+1) !=' '){
if(last_Index!=i-1){
wordCounter++;
}
last_Index=i;
}
}
}
}
return wordCounter;
}
public static int countUpperCases(String text) {
int upper = 0;
char[] compare1 = text.toCharArray();
char[] compare2 = text.toUpperCase().toCharArray();
for (int i = 0; i < compare1.length; i++) {
if (compare1[i] != compare2[i])
upper++;
}
return upper;
}
public static int countWords(String text) {
String pattern = "[.|:|;|?|!|,|'| ]";
int counter = 0;
for (String word : text.split(pattern)) {
if (word.length() != 0)
counter++;
}
return counter;
}
public static int countSentecens(String text) {
String pattern = "[.|?|!]";
int counter = 0;
for (String word : text.split(pattern)) {
if (word.length() != 0)
counter++;
}
return counter;
}
public static int countVowels(String text) {
int vowels = 0;
for (char c : text.toCharArray()) {
switch (c) {
case 'a':
vowels++;
case 'e':
vowels++;
case 'i':
vowels++;
case 'o':
vowels++;
case 'u':
vowels++;
}
}
return vowels;
}
这个回报:
文本:
Java 是一种计算机编程语言,它是并发的、基于类的、面向对象的,并且专门设计为具有尽可能少的实现依赖关系。它旨在让应用程序开发人员“编写一次,随处运行”(WORA),这意味着在一个平台上运行的代码无需重新编译即可在另一个平台上运行。Java 应用程序通常被编译为可以在任何 Java 虚拟机 (JVM) 上运行的字节码(类文件),而与计算机架构无关。截至 2012 年,Java 是最流行的编程语言之一,特别是对于客户端-服务器 Web 应用程序,据报道有 900 万开发人员。[10][11] Java 最初由 Sun Microsystems(后来并入 Oracle Corporation)的 James Gosling 开发,并于 1995 年作为 Sun Microsystems 的核心组件发布。Java平台。该语言的大部分语法源自 C 和 C++,但它的低级设施比它们中的任何一个都少。原始和参考实现 Java 编译器、虚拟机和类库由 Sun 从 1991 年开始开发,并于 1995 年首次发布。截至 2007 年 5 月,按照 Java 社区进程的规范,Sun 重新授权其大部分 Java 技术GNU 通用公共许可证。其他人也开发了这些 Sun 技术的替代实现,例如 GNU Compiler for Java(字节码编译器)、GNU Classpath(标准库)和 IcedTea-Web(小程序的浏览器插件)。虚拟机和类库由 Sun 从 1991 年开始开发,并于 1995 年首次发布。截至 2007 年 5 月,根据 Java Community Process 的规范,Sun 根据 GNU 通用公共许可证重新授权其大部分 Java 技术。其他人也开发了这些 Sun 技术的替代实现,例如 GNU Compiler for Java(字节码编译器)、GNU Classpath(标准库)和 IcedTea-Web(小程序的浏览器插件)。虚拟机和类库由 Sun 从 1991 年开始开发,并于 1995 年首次发布。截至 2007 年 5 月,根据 Java Community Process 的规范,Sun 根据 GNU 通用公共许可证重新授权其大部分 Java 技术。其他人也开发了这些 Sun 技术的替代实现,例如 GNU Compiler for Java(字节码编译器)、GNU Classpath(标准库)和 IcedTea-Web(小程序的浏览器插件)。
字数:230
句子:9
元音:1597
字符:1516
大写:1154
你的话数:230