2

My code is:

import java.util.Scanner;

class mainClass {
    public static void main (String [] args) {          
        secondaryClass SCO = new secondaryClass();          
        Scanner scanner = new Scanner(System.in);           
        String randomtext = scanner.nextLine();    
        if(randomtext.equals("What is the time"))
        {
            SCO.giveTime();               
        }
        else if (randomtext.equals("Whats the time"))
        {       
            SCO.giveTime();             
        }
    }       
}

I would like to know if I could replace that if else statement with some thing along the lines of:

import java.util.Scanner;

class mainClass {
    public static void main (String [] args) {  
        secondaryClass SCO = new secondaryClass();      
        Scanner scanner = new Scanner(System.in);       
        String randomtext = scanner.nextLine();
        if(randomtext.equals("What is the time" || "Whats the time"))
        {
            SCO.giveTime();
        }       
    }
}

SCO is the object for my second class by the way, it outputs the time perfectly.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Aazim Abdul
  • 59
  • 2
  • 6

6 Answers6

4

You can one comparison using regex, but it only moves the OR from java to regex:

if (randomtext.matches("(What is the time)|(Whats the time)"))

Although you can express it more succinctly:

if (randomtext.matches("What(s| is) the time"))

and even make an apostrophe and/or a question mark optional:

if (randomtext.matches("What('?s| is) the time\\??"))
Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

You need to phrase it like this:

if (randomtext.equals("What is the time") || randomtext.equals("Whats the time"))
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
1

You are right in using the || logical-OR operator, but wrong in how you are using it. Take each specific condition in the if and else if from your first example and place || in between them in just one if with no else if necessary.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

The most obvious way would be to do this:

if(randomtext.equals("What is the time") || randomtext.equals("Whats the time"))
{
      SCO.giveTime();
}

but since JDK 7 you can use a switch statement:

switch (randomtext) {
    case "What is the time":
    case "Whats the time":
        SCO.giveTime();
        break;
}
jabbink
  • 1,271
  • 1
  • 8
  • 20
1

Another look:

import java.util.Scanner;

class mainClass {
    public static void main (String [] args) {
        secondaryClass SCO = new secondaryClass();

        Scanner scanner = new Scanner(System.in);

        String randomtext = scanner.nextLine();

        List<String> stringsToCheck = new ArrayList<String>();
        stringsToCheck.add("What is the time");
        stringsToCheck.add("Whats the time");

        if (stringsToCheck.contains(randomtext)) {
              SCO.giveTime();
        }       
    }
}   
Mustafa Genç
  • 2,569
  • 19
  • 34
-1

Assuming you only have two possible options in a conditional statement, you can use this

randomtext = Condition1 ? doesThis() : doesThat();

p.s. I wouldn't do "cases". In this case it's not important, because it is only two options but when you use cases, each case-line will be checked individually against the conditional "TRUE" and that could take a long time on long programs.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
  • I downvoted you because you misunderstood the question - a conditional statement does not help in this case. And IIRC I got an A+ in Java 101 :) – Blorgbeard May 16 '13 at 02:53
  • And I am a senior Java programmer at LOTUS Marketing Solutions. And I'm telling you it does work. But I don't have time for this. So continue thinking you're right and misleading other people. This site is not about who knows and who helps. This site is full of immaturity and targeted downvoting. :waiting to be suspended: – LOTUSMS May 16 '13 at 12:40
  • Conditional statements work, but they don't help in this case. How are you at reading comprehension? Also, "case" statements are [fast](http://stackoverflow.com/a/767849/369). And quit whining, you're not going to be "suspended". You don't sound like a senior dev to me. – Blorgbeard May 16 '13 at 20:53
  • You're welcomed to come work for us if you'd like. And, too late...Just got suspended ...this is what moderators with no experience straight out of high school do ;) – LOTUSMS May 16 '13 at 21:39