-1

I have to do a recommend-a-laptop project for school, and I want to keep it as concise as possible by using very long if statements with multiple conditions. I have many of these, but here's one of them:

if 
      ((questionArray [0].equals ("A") && (questionArray [1].equals ("A") || ("B") || ("C") || ("D")) && questionArray [2].equals ("A") && questionArray [3].equals ("A") && questionArray [4].equals ("A") || ("B") || ("C") || ("D")) 
{


System.out.println("ASUS Laptop E200HA-UB02-GD 11.6 inch");

}

I am constantly getting, Error: '(' expected and I can't seem to figure it out. Please help!

John Joe
  • 12,412
  • 16
  • 70
  • 135
APSmoove
  • 3
  • 1
  • 1
    to put it simply, you can't do this --> ("A") || ("B") || ("C") || ("D")) ; – Ousmane D. Mar 23 '17 at 02:01
  • Correction, these are the errors I'm getting: Error: bad operand types for binary operator '||' first type: boolean second type: java.lang.String – APSmoove Mar 23 '17 at 02:02
  • 1
    After `||` , you need to write `questionArray [4].equals("B")...` – John Joe Mar 23 '17 at 02:04
  • 1
    Thanks a lot for the help everyone, it worked! :) – APSmoove Mar 23 '17 at 02:31
  • "I want to keep it as concise as possible by using very long if statements with multiple conditions" --- This is making your life harder, not easier. (For example, you've got an extra `(` hiding in that mess of a conditional.) Write a separate method for each attribute you're querying, or at least a separate `if` statement. – Kevin J. Chase Mar 23 '17 at 03:14
  • Also, if you intend those "A or B or C or D" conditionals to mean "any response is OK", you can simply remove them --- there's no need to test `questionArray[1]` or `questionArray[4]` at all if _all_ values pass the test. (If there are 5 or more possible values, this won't work.) – Kevin J. Chase Mar 23 '17 at 03:17

1 Answers1

1

After || , you need to write questionArray [4].equals("B")...

The complete code

 if ((questionArray[0].equals("A")) && (questionArray[1].equals("A"))
                || (questionArray[1].equals("B")) || (questionArray[1].equals("C"))
                || (questionArray[1].equals("D")) && (questionArray[2].equals("A"))
                && (questionArray[3].equals("A")) && (questionArray[4].equals("A"))
                || (questionArray[4].equals("B")) || (questionArray[4].equals("C"))
                || (questionArray.equals("D"))) 
  {

      System.out.println("ASUS Laptop E200HA-UB02-GD 11.6 inch");
   } 

The error gone now but I'm not sure whether this code wii work since I never tried mixing the || operator with the && operator.

John Joe
  • 12,412
  • 16
  • 70
  • 135