Which of the following expressions can be inserted in the above code so that the validateInput method will return true if and only if the input string contains non-whitespace data?
I understood the question as meaning "return true if the string contains any non-whitespace data". This means that if it should return true on, for example " t e s t " since it contains non-white space data.
Did you mean to ask "will return true if the input string contains non-whitespace data and only non-whitespace data?"
I understood the question as meaning "return true if the string contains any non-whitespace data". This means that if it should return true on, for example " t e s t " since it contains non-white space data.
Yes, you understood correctly. Yes, " t e s t " should return true.
The "only if" of the "if and only if" part is to convey that a string containing only whitespace data should not cause true to be returned.
>Did you mean to ask "will return true if the input string contains non-whitespace data and only non-whitespace data?"
No.
If you like our products and services, please help us by posting your review here.
"whitespace" means characters such as space, tab, new line. Non-whitespace means the opposite.
As per the problem statement, the validateInput(String str) method should return true if and only if the input string contains non-whitespace data. Meaning, the following strings should cause the validateInput method to return true:
"test" <- no whitspace at all
" t e s t " <- some whilespace but non-whitespace chars are also present
and the following strings should cause the validateInput method to return false:
"" <- empty string. Both isBlank and isEmpty will return true.
" " <- the string is not empty but there is no non-whitespace in this string. isBlank will return true but isEmpty() will return false, and !str.isEmpty() will be true, which is not what we want.
So, !str.isBlank() is the only correct option.
If you like our products and services, please help us by posting your review here.