1 Attachment(s)
Beginner Java - Bug in my code?
Hi, I'm a beginner in java.
I have to write a program that takes a string containing a sentence or a set of sentences, and counts the number of words in the sentence that meet or exceed a specified minimum length (in letters). For example, if the minimum length entered is 4, your program should only count words that are at least 4 letters long.
Input the string and the minimum word length (integer), in that order, and output the word count (integer). Words will be separated by one or more spaces. Non-letter characters (spaces, punctuation, digits, etc.) may be present, but should not count towards the length of words.
This is the code that I have written:
Code :
public class WordCount {
public static void main(String [] args){
System.out.println("Enter a sentence:");
String sentence = IO.readString();
System.out.println("Enter the word length limit");
int limit = IO.readInt();
int sentencelength = sentence.length();
System.out.println("The sentence length is:" + sentencelength);
int lettercount = 0;
int finalcount = 0;
for (int i=0; i < sentencelength; i++){
if (sentence.charAt(i) == ' '){
if (lettercount >= limit){
finalcount ++;
lettercount = 0;
}
}else{
if(Character.isLetter(sentence.charAt(i))){
lettercount ++;
}
}
}
System.out.println("The number of words with a length of" + limit);
IO.outputIntAnswer(finalcount);
}
}
If I input the string "The cow jumped over the barn and under the moon" and I input a limit of 2, my output should be 10. But my program is outputting 9. I figured out [by using system.out.println()]that my program does not count the letters of the last word in a sentence. How do I fix this bug?
Sorry, I'm new to this forum. I didn't know how to post my code. Is this better?
Re: Begginer Java - Bug in my code?
If you want help, you'll have to provide an SSCCE, not a screenshot. Don't forget the highlight tags.
Re: Begginer Java - Bug in my code?
The problem with ur code is it uses " " (space) to increment the finalcount . There is no space after last word, so it doesn;t gets count. Try appending a space to the string after u take as input or better modify ur else bracket code like this =>
Code Else:
else{
if(Character.isLetter(sentence.charAt(i))){
lettercount ++;
}
if ((lettercount >=limit)&&(i == sentencelength-1)) // this will get u the last word
{
finalcount++;
}
}
Re: Begginer Java - Bug in my code?
Hi Aju,
Thanks! Your solution works for the case I gave above (input string:"The cow jumped over the barn and under the moon" and input: limit = 2). But it doesn't work if i set the limit = 4 for the same string (I should get an output of 5, but the program will output 6).
Code :
for (int i=0; i < sentencelength; i++){
if (sentence.charAt(i) == ' '){
if (lettercount >= limit){
finalcount ++;
lettercount = 0;
}
}else{
if(Character.isLetter(sentence.charAt(i))){
lettercount ++;
}
if ((lettercount >= limit)&&(i == sentencelength-1)){
finalcount++;
}
}
}
How would i fix this?
Re: Begginer Java - Bug in my code?
Trace through your program with a debugger, or even some print lines, to figure out where the program flow differs from what you expect the program to do. Walk through it with a piece of paper and a pencil, comparing what you expect to happen to what actually happens. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
Re: Begginer Java - Bug in my code?
oops check this
Code Java:
for (int i=0; i < sentencelength; i++){
if (sentence.charAt(i) == ' '){
if (lettercount >= limit){
finalcount ++;
}
lettercount = 0;
}else{
if(Character.isLetter(sentence.charAt(i))){
lettercount ++;
}
if ((lettercount >= limit)&&(i == sentencelength-1))
{
finalcount++;
}
}
}
the thing to do is move lettercount = 0 out of the if (lettercount >= limit) condition loop
because irrespective of the count greater or equal to limit,
after every space, the counting of letters should begin with 0.
Now it will work for all limits.
Re: Begginer Java - Bug in my code?
I actually figured it out, before I checked back on here. But thank you!!
Re: Begginer Java - Bug in my code?