-4

I am trying to do the following expression in java:

     String name = request.getParameter("name");
     String email = request.getParameter("email");
     String password = request.getParameter("password");

     if (!name || !email || !password) {
         model.addAttribute("error", "Enter all fields please!");
     }
    return "register";

However, I get an error that complains about the ! operator. What would be the correct way to do the above if statement?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 3
    The above code is correct Java if `name`, `email`, and `password` are all **`boolean`** variables. Since they are probably `String` variables, what did you expect the code to do? Java is not JavaScript, and the `!` operator only works on boolean values. – Andreas Jul 04 '18 at 20:56
  • @Andreas please see updated code: my guess is it has to do with checking the string value. – David542 Jul 04 '18 at 20:57
  • `!name` makes no sense whatsoever. What are you trying to do with that? You can only negate boolean expressions, not Strings –  Jul 04 '18 at 20:58
  • You *guess* is it "has to do with string value"????? Didn't you write the code? What did *you* intend the code to do? *Why* did you think `!` on a `String` is valid? – Andreas Jul 04 '18 at 20:58
  • `if(name!=null && !name.isEmpty()){` – achAmháin Jul 04 '18 at 20:58

2 Answers2

0

If you can use third party library, you can use StringUtils class as below:

if (StringUtils.isEmpty(name) || StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
     model.addAttribute("error", "Enter all fields please!");
 }

Details available at : https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

sauumum
  • 1,638
  • 1
  • 19
  • 36
-2

If using a string, checking whether it is empty or not can be done as follows:

 if (name.isEmpty()|| email.isEmpty() || password.isEmpty()) {
     model.addAttribute("error", "Enter all fields please!");
 }

To check whether it is empty or null, you can do the following:

import org.springframework.util.StringUtils;

 if (StringUtils.isEmpty(name)|| StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
     model.addAttribute("error", "Enter all fields please!");
 }
David542
  • 104,438
  • 178
  • 489
  • 842
  • 2
    might also need to check for `null` if the values are absent – Lino Jul 04 '18 at 20:59
  • 2
    You’ve answered your own question despite apparently not knowing what was going on. You should add a `!=null` check too. – achAmháin Jul 04 '18 at 21:00
  • [`StringUtils.isEmpty(String)`](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isEmpty(java.lang.String)) from Apache commons library is useful for this or its counterpart `StringUtils.isNotEmpty(String)`. – d.j.brown Jul 04 '18 at 21:01
  • 2
    Providing a self answer to such a common question is not really liked here. You *should* know that according to your reputation... – Lino Jul 04 '18 at 21:04