-1
import java.util.Scanner;

public class Outside{
 public static void main (String[] args){
    Scanner scan = new Scanner (System.in);
    
    System.out.print("Is your homework done? ");
    String homework;
    homework = scan.nextLine();
    String answer1;
  answer1 = No||no||NO||No ||no ||NO ;
    if (homework.equals(answer1)){
        System.out.print("Do your homework");
    }//If Homework
 }//main
}//outside

I'm getting an error at the No||...||NO ; section. What I'm currently trying to do is make it say "Do your homework" if it gets any "no" response. I tried using if (homework.equals(No||...||NO)) and it gives the same error. What statement should I use?

  • Also, there is no need it declare and initialize your variables on different lines. `String homework = scan.nextLine();` works just fine – QuinnFreedman Mar 07 '21 at 01:15

1 Answers1

0

In your code, answer1 is a string. Strings can only hold a sequence of characters... nothing else. If you want to capture the idea that it could be one of a few different strings, you will need to use a different data structure.

Option 1: list of strings

The option that is most in the spirit of what you are asking is this:

List<String> negativeAnswers = Arrays.asList("No", "no", "NO", "No ", "no ", "NO ");
if (negativeAnswers.contains(homework)) {
    // homework was one of the negative answers
}

Option 2: case-independent comparison

The better option for this particular case is just to ignore capitalization and spaces when you are doing your comparison.

if (homework.toLowerCase().strip().equals("no") {
    // homework was similar to "no"
}

toLowerCase() converts the string to all lowercase and strip() removes all the whitespace. If you are using an old version of Java, you will have to use trim() instead.

QuinnFreedman
  • 2,242
  • 2
  • 25
  • 42
  • I'll definitely keep this for later use, but I'm currently in a CS class. Within the class (https://www.w3schools.com/java/java_operators.asp using this as a reference on where my class is at) I'm up to if..else + while (My class also learned of import). We haven't started to using the terms your recommending and would likely affect my grade with this assignment – Chukwuemeka Mar 07 '21 at 20:16
  • Thank you for showing me these new statements to use, I'll definitely keep them in mind – Chukwuemeka Mar 07 '21 at 20:17
  • If you just want to use basic if..else, then you could do something like `if (homework.equals("no") || homework.equals("NO") || homework.equals("No") ... )` etc – QuinnFreedman Mar 07 '21 at 20:30