-5

I didn't know how to explain the question but hopefully this will make you understand

I know this code does not work

a = "hello";
    if (a.equals("hello" || "greetings")){
        //Execute code
    }

Is there a simple way to do this without an error

I could do this but this means that i will need to repeat codes on both

a = "hello";

    if (a.equals("hello")){
        //Do code
    }
    if (a.equals("greetings")){
        //Execute code
    }

This is my current code but its not what i want it to do. What i want it to do is if for example topic = "Whats the date"; i want it to execute the code because it contains date, I cant find a way to check if it contains date and so the || works

        scanner = new Scanner(System.in);
        String topic = scanner.nextLine();
        topic = topic.toLowerCase();

    if (topic.equals("time") || topic.equals("date")){
        System.out.println("The time is " + Calendar.getInstance().getTime());
    }
mateos
  • 1,405
  • 1
  • 17
  • 26
  • 1
    There are plenty of examples, check the link. [see the .equals() ][1] [1]: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – ohadsas Sep 05 '15 at 14:50
  • 1
    @GurfX You should really read some more of Java programming. The code you posted contain several syntax errors. – MC Emperor Sep 05 '15 at 14:54
  • @MCEmperor I know i only used that code so you could understand my question. I knew the code didn't work – mateos Sep 05 '15 at 15:01
  • Java has a contains method, no? – D. Ben Knoble Sep 05 '15 at 15:20
  • Yes it does, In fact i just retried the code that didn't work last time and it somehow worked, I will answer my question with it – mateos Sep 05 '15 at 15:29

4 Answers4

4

You code won't compile since the syntax is not correct in any case.

The answer is no, || operator works on boolean expressions and a String is not a boolean value.

The meaningful way to do it is the traditional way:

if (a.equals("hello") || a.equals("greetings") {
  ..
}

If you have many different alternatives for many different tokens then you should consider using a different approach, like a Map<String, Consumer<String>> so that you can do something like:

Map<String, Consumer<String>> mapping = new HashMap<>();
Consumer<String> greetings = s -> code;

mapping.put("hello", greetings);
mapping.put("greetings", greetings);

...

Consumer<String> mapped = mapping.get(a);

if (mapped != null)
  mapped.accept(a);
Jack
  • 131,802
  • 30
  • 241
  • 343
3
if (yourString.equals("hello") || yourString.equals("greetings")){
    //Execute code
}

If you want to compare your string with ignoring case. Use equalsIgnoreCase() method instead of equals()

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

The previous answers are correct and on point, but I'd like to change one thing just a bit:

if ("hello".equals(a) || "greetings".equals(a)) {
    //...
}

That way, you are not risking NullPointerException (which would get thrown if a is null, because you can't execute a method on a null object). If you want to ignore the case, use equalsIgnoreCase(String).

Or, alternatively, use switch:

switch(a) {
    case "hello": //do something
            break;
    case "greetings": //do something
            break;
    default: //handle case where there is no match, if needed
}
Luke
  • 1,284
  • 1
  • 12
  • 34
  • using switch will still make me repeat the code, i'm looking to combine them together – mateos Sep 05 '15 at 15:11
  • Yeah, in your case it will. Also, depending on the context, you may find `"string literal".equals(string_var)` useful, though with the `Scanner` and/or `System.in` it makes no difference – Luke Sep 05 '15 at 15:19
-1

I found an answer to my question after some experimenting, Sorry about the confusion

        scanner = new Scanner(System.in);
        String topic = scanner.nextLine();
        topic = topic.toLowerCase();

        if (topic.contains("time") || topic.contains("date")){
        System.out.println("The time is " + Calendar.getInstance().getTime());
    }
mateos
  • 1,405
  • 1
  • 17
  • 26