Reputation: 31
The program asks for string input, then a minimum word length input. If the word is less than the minimum word length, it will not count. I'm trying to do the program without using split() or whatever every other post says to do for these problems.
I thought maybe I have to check the first word, last word, and then words in between. But I'm having trouble. I spent way too long on this and I can't figure it out.
IO is a different class used to input, output, etc.
public class WordCount {
public static void main(String[] args) {
int minLength;
System.out.println("Enter a string.");
String string=IO.readString();
String str1=string.toLowerCase(); //changes all characters in the string to lower case
String str=str1.trim(); //Trims all spaces in the end.
System.out.println("Enter minimum length for each word.");
minLength=IO.readInt();
int num=wordCount(str, minLength);
IO.outputIntAnswer(num);
}
public static int wordCount(String str, int minLength){ //Method that counts words.
int count=0;
// First Word
for(int i=0;i<str.length();i++){
if (str.charAt(i)==' '){
String firstWord=str.substring(0,i);
if (firstWord.length()>=minLength){
count++;
}
}
}
// Last Word
for (int i=str.length()-1;i>0;i--){
if(str.charAt(i)==' '){
String lastWord=str.substring(i, str.length()-1);
if(lastWord.length()>=minLength){
count++;
}
for (int j=0; j<i;j++){ //An attempt to find other words in between..does not work
if(str.charAt(j)==' '){
String word=str.substring(j+1, str.indexOf(' ', j+1));
if(word.length()>=minLength){
count++;
}
}
}
}
}
return count;
}
}
Upvotes: 0
Views: 4135
Reputation: 91
Please try this. This takes care of extra spaces, leading, & also trailing spaces.
public class WordsCount {
static String str = " I Love Java for its oops concepts ";
static int i;
static int count = 0;
public static int numOfWords(String str) {
int length = str.length();
for(i=0;i<length;) {
if(str.charAt(i)!=' ') {
while(str.charAt(i)!=' ') {
i++;
if(i>=str.length())
break;
}
count = count+1;
} else {
i++;
}
}
return count;
}
public static void main(String[] args) {
count = numOfWords(str);
System.out.println("Count of words is : "+count);
}
}
Upvotes: 0
Reputation: 3221
This should be enough:
public static int wordCount(String str, int min) {
int count = 0;
str = str.trim()+" ";
for(int i=0, st=0; i<str.length(); i++) {
if(str.charAt(i) != ' ') continue;
if(i-st >= min) count++;
st = i+1;
}
return count;
}
Upvotes: 2
Reputation: 761
Beside the fact that your code is kind of scrappy and inadequate, the main problem here is that your first FOR loop doesn't stop after finding the first word. In fact, in counts every word in the input string, minus 1.
There is the same problem in your second loop.
If you really want to do it this way, you should use the BREAK keyword, to stop the loops when necessary.
EDIT
This should work
public static int wordCount(String str, int minLength) { //Method that counts words.
int count = 0;
for (int i = 0, wordLength = 0; i < str.length() + 1; i++) {
if (i == str.length() || str.charAt(i) == ' ') {
if (wordLength >= minLength) {
count++;
}
wordLength = 0;
} else {
wordLength++;
}
}
return count;
}
Upvotes: 1