I have a boolean method that is doing some error checking on a string. I have a int class Constant called "numWords" = 8. I pass a string into the boolean method where I do a word count on the string using .split and .length. An int called "words" counts the number of words in the string. After this is done I want to do an if statement comparing words against numWords. If they are equal in number return true else false. I have tried == and .equals but nothing works. Any ideas?
public static final int numWords = 8;
boolean test = true;
String sentence = "I am a man";
test = check(sentence);
public static boolean check (String sentence) {
boolean x = true;
String[] s = sentence.split(" ");
int words = s.length;
if (words == numWords) {
x = true;
}
else {
x = false;
}
return x;
}
I have tried == , it never evaluates to false
I have tried .equals, int cannot be dereferenced
I have tried =, doesn't compile incompatible types
What I want is if words = 8 then it equals numWords and returns true. If words is 7 or 9 then it doesn't equal numWords and returns false.
boolean? in yourcheckmethod? Post compilable code please. - skiwireturnstatement in your method? If there is noreturnstatement, that code will not compile. - merlin2011;here:boolean x = true- StoopidDonutfalse, we can assume he had a return statement there, but just missed in the code here. - Rohit Jain