Question:
What is wrong with this java method?
kfneil
2013-10-25 15:58:03 UTC
The question is this: Write a method called wordCount that accepts a String as its parameter and returns the number of words in the String. A word is a sequence of one or more nonspace characters (any character other than ' '). For example, the call wordCount("hello") should return 1, the call wordCount("how are you?") should return 3, the call wordCount(" this string has wide spaces ") should return 5, and the call wordCount(" ") should return 0.


My code is this:

public static int wordCount(String inputString) {
// write your code to count the word in the inputString
int count = 0;
boolean word = false;
int space = ' ';
char last = inputString.charAt(inputString.length() - 1);
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == ' ') {
count = 0;
} else if ((inputString.charAt(i + 1) == ' ' || (inputString.charAt(i + 1) == last) {
count++;
}
}
return count;
}

I feel super dumb that I've been working on this for as long as I have but I can't seem to get it to work.
Three answers:
?
2013-10-25 17:08:16 UTC
public static int wordCount(String inputString) {

// write your code to count the word in the inputString

int count = 0;

boolean word = false;

int space = ' ';

char last = inputString.charAt(inputString.length() - 1);

for (int i = 0; i < inputString.length(); i++) {

if ( (inputString.charAt(i ) != ' ') &&( (inputString.charAt(i+1)==' ') || (i+1)== last)) ) count++;



}

return count;

}
anonymous
2013-10-25 23:15:14 UTC
} else if ((inputString.charAt(i + 1) == ' ' || (inputString.charAt(i + 1) == last) {



This line is missing a closing parentheses. That's the only issue I had compiling, as fixing this issue allowed the rest of the code to compile.



Edit: There are some issues I ran into when running your code though. Namely an out of bounds exception being thrown when I tested a simple string like "hello"



I did some debugging, and looking at your method again, I offer these tips to you:



1- start a counter at 0, and have a boolean variable to keep track of whether you hit a space or not for this particular method of counting a word



(probably easier to use substring, but I was able to see what you were trying to do)



2- I was confused by your loop logic. This is what I got to work:

for 0 to length of the string

{

if the char at this location is not a space:

{

if the flag indicates this is a new word

{

increment count

set the flag to false

}

}else (the char is a space)

set the flag to true

}end for loop



Let's test this on the string "hello"

Starting at the beginning, we see that char 0 is h thus not a space. And since our flag is true, this is a new word, so we increment our counter. Then we set our flag to false.

Move to char 1 is e, which is not a space, but our flag says this is not a new word, so nothing happens

Move to char 2 is l, which is not a space, but again, the flag indicates we are not at a new word. Nothing happens again.

This will continue for the other l, and the o. Our count remains at 1, which is what we expect. To see if it works on other strings test out the others you posted: wordCount("how are you?") should return 3

wordCount(" this string has wide spaces ") should return 5

wordCount(" ") should return 0



This was my method, but there are other ways of getting a word count. This one uses some ideas already present in your code so you may want to try it 1st. Good luck.
Puter_Notgeek
2013-10-25 23:42:34 UTC
It complies fixing the (), but its going out of bounds with the i+1.



I think it might be easier to count the words using substring...when its not equal to space, move past until space, count.

Looking at it now.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...