This is for a project, most of it is finished, but I need to be able to count the amount of words there are in a String or file. I have to use a nested for loop, and I have to use a String containing the delimiters for a word. Right now this is what I have:
public static int wordCounter(String text)
{
String WORDS_GROUP = ",\n ";
String text= "This is my sample text";
int wordCount=0;
for(int i=0; i<text.length(); i++){
for(int j=0; j<WORDS_GROUP.length(); j++){
if(text.charAt(i)==WORDS_GROUP.charAt(j)){
wordCount++;
}
}
}
}
如果您绝对需要使用嵌套的for循环,则可以让第一个for循环遍历文本中的所有行,第二个(嵌套)for循环遍历每行的所有单词,然后简单地对它们进行计数。
You have increment wordCount
only if the last char does not belong to WORDS_GROUP
otherwise you end up with more words than you actually have. For example when you find the first space after sample
you'll have wordCount
is 3 but then comes another space, your algorithm will increment again and this obviously not correct.
You could introduce a new boolean
variable which you set to true
if you find the first withe space and do the first increment. Then set it to false when you find a non white space Increments happen only when that variable is set to false
.
You can maintain a boolean that represents your "state". Are you in a block of spaces (true), or are you in a block of non-spaces (false)?
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
String WORDS_GROUP = ",\n ";
String text = "This is my sample text";
int wordCount = 0;
boolean previousCharWasSpace = true;
for (int i = 0; i < text.length(); i++) {
boolean thisCharIsASpace = false;
for (int j = 0; j < WORDS_GROUP.length(); j++) {
if (text.charAt(i) == WORDS_GROUP.charAt(j)) {
previousCharWasSpace = true;
thisCharIsASpace = true;
break;
}
System.out.println("char=" + text.charAt(i) + " j=" + j
+ " previousCharWasSpace=" + previousCharWasSpace);
}
if (!thisCharIsASpace && previousCharWasSpace) {
wordCount++;
previousCharWasSpace = false;
System.out.println("char=" + text.charAt(i)
+ " previousCharWasSpace=" + previousCharWasSpace
+ " wordCount=" + wordCount);
}
}
System.out.println("wordCount=" + wordCount);
}
}
Output:
char=T j=0 previousCharWasSpace=true
char=T j=1 previousCharWasSpace=true
char=T j=2 previousCharWasSpace=true
char=T previousCharWasSpace=false wordCount=1
char=h j=0 previousCharWasSpace=false
char=h j=1 previousCharWasSpace=false
char=h j=2 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=false
char=i j=1 previousCharWasSpace=false
char=i j=2 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=true
char=i j=1 previousCharWasSpace=true
char=i j=2 previousCharWasSpace=true
char=i previousCharWasSpace=false wordCount=2
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=true
char=m j=1 previousCharWasSpace=true
char=m j=2 previousCharWasSpace=true
char=m previousCharWasSpace=false wordCount=3
char=y j=0 previousCharWasSpace=false
char=y j=1 previousCharWasSpace=false
char=y j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=true
char=s j=1 previousCharWasSpace=true
char=s j=2 previousCharWasSpace=true
char=s previousCharWasSpace=false wordCount=4
char=a j=0 previousCharWasSpace=false
char=a j=1 previousCharWasSpace=false
char=a j=2 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=false
char=m j=1 previousCharWasSpace=false
char=m j=2 previousCharWasSpace=false
char=p j=0 previousCharWasSpace=false
char=p j=1 previousCharWasSpace=false
char=p j=2 previousCharWasSpace=false
char=l j=0 previousCharWasSpace=false
char=l j=1 previousCharWasSpace=false
char=l j=2 previousCharWasSpace=false
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char=t j=0 previousCharWasSpace=true
char=t j=1 previousCharWasSpace=true
char=t j=2 previousCharWasSpace=true
char=t previousCharWasSpace=false wordCount=5
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char=x j=0 previousCharWasSpace=false
char=x j=1 previousCharWasSpace=false
char=x j=2 previousCharWasSpace=false
char=t j=0 previousCharWasSpace=false
char=t j=1 previousCharWasSpace=false
char=t j=2 previousCharWasSpace=false
wordCount=5
You can play with this here: http://ideone.com/ASEjwB
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.