1
votes

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.

4
Where do you return a boolean? in your check method? Post compilable code please. - skiwi
Where is the return statement in your method? If there is no return statement, that code will not compile. - merlin2011
What input are you giving? - Rohit Jain
A note: wouldn't compile as it misses a ; here: boolean x = true - StoopidDonut
Why was this even downvoted? Also since OP is claiming the method is always returning false, we can assume he had a return statement there, but just missed in the code here. - Rohit Jain

4 Answers

1
votes

I am making a guess that what you actually want is to return a boolean instead of assigning a boolean.

public static boolean check (String sentence) {

    String[] s = sentence.split(" ");
    int words = s.length;

    return words == numWords
}
1
votes

You don't have valid syntax (any statement not having a semi-colon is invalid), and you're not actually returning anything.

Without anything to the correctness of your program, add a semi-colon after you declare and instantiate x (so boolean x = true;), and also add return x; to the bottom of your method. Or, you could simply perform this:

return s.length == numWords;

No if-checks required, and it will be true if they're equivalent, and false otherwise.

0
votes

First of all, your code won't compile if there is a missing colon in

boolean x = true;

Also, since your method declaration is

public static boolean check (String sentence)

you have to return a boolean:

return some_boolean;

The final code would be:

public static boolean check (String sentence) {
    String[] s = sentence.split(" ");

    return s.length == numWords;
}

Note:

The line

return words == numWords;

will return the boolean that is result of the comparison s.length == numWords.

0
votes

Make it return the number of your words, then check it outside your method if it equals your numWords value. Or make another method that does that.