-2

I'm taking a introduction course in Java and I have a question about the ? expression (I've done some C# programming) but I'm not that familiar with it there either.

If I have this if statement, how do I use the ? on it? I have a task where I'm supposed to compare three variables in different ways and I hate to have alot of if statements.

    //Task b (If var3 is divisible with var2)
    if(var3 % var2 == 0)
    {
        System.out.println("\n" + var3 + " is divisible with " + var2);
        System.out.println(var3 + " divided with " + var2 + " is: " + var3 / var2);
    }
    else
    {
        System.out.println("\n" + var3 + " is not divisible with " + var2);
    }

This just look messy.. It's okay to turn it in like this but I personally think it look awful.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Fredrik
  • 75
  • 8
  • 4
    No, it doesn't look messy. – Maroun Jun 10 '15 at 09:03
  • Why do you think using the ternary operator would make this look less messy? – dhke Jun 10 '15 at 09:04
  • This sounds a bit like an XY Problem. Do you actually want to avoid long chains of if statements? It honestly sounds like what you're really looking for is a `switch` – Dragondraikk Jun 10 '15 at 09:05
  • 6
    I think it looks ok. If you use the terny operator `?` it will be bad readable. The terny operator sould only be used in very short terms. – Jens Jun 10 '15 at 09:06
  • @MarounMaroun When you have 7 ones that look almost exactly the same I think it does. Jens Alright. Thank you. Ill stick to what I have then. – Fredrik Jun 10 '15 at 09:06
  • 2
    @Fredrik Then `?` won't help you here. Consider arrays and loops. – Maroun Jun 10 '15 at 09:07
  • 1
    It would be useful if you would show a situation which is *actually* a problem then, instead of one that isn't. – Jon Skeet Jun 10 '15 at 09:07

6 Answers6

2

You could use it like so:

bool isDivisible = var3 % var2 == 0;

System.out.format("\n%d is %sdivisible with %d",
                   var3,
                   isDivisible ? "" : "not ",
                   var2);
if (isDivisible) {
    System.out.format(" %d divided with %d is %d", var3, var2, var3 / var2);
}

I'm using it to inject the word "not" where required, so that the first two statements are now combined. Note use of format as string concatenation gets ugly quickly.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
weston
  • 54,145
  • 21
  • 145
  • 203
  • Sorry, edited (and rolled back) the answer because didn't realize `printf()` actually calls `format()`. – Kayaman Jun 10 '15 at 09:56
  • That's OK `printf` probably more readable actually. Though OP: you should consider also `System.out.println(String.format(...))` as will add new line characters for you. – weston Jun 10 '15 at 10:30
  • 1
    @weston I prefer the simpler `%n` for a new line, e.g., `System.out.format("this %d and that %d%n", 1, 2)` – Hummeling Engineering BV Jun 14 '15 at 21:03
1

? is not complete, it is a group of ? : termed as ternary operator. Basically the syntax is as follows:

<variable> = <condition> ? <true value> : <false value>;

So your code goes like this:

String msg = 0 == var3 % var2 ?
            "\n" + var3 + " is divisible with " + var2 :
            "\n" + var3 + " is not divisible with " + var2;
System.out.println(msg);

You could also use () to avoid confusion:

String msg = (0 == var3 % var2) ?
                ("\n" + var3 + " is divisible with " + var2) :
                ("\n" + var3 + " is not divisible with " + var2);
System.out.println(msg);
0

It is called ternary operator. It can be used to evaluate logical expression in a statement.Its syntax is

[condition] ? [true expression] : [false expression]

In your code example,

public static void main(String[] args) {
    int var3 = 12;
    int var2= 6;
    System.out.println( var3%var2==0 ? "divisible" : "not divisible" );   }
Sarit Adhikari
  • 1,344
  • 2
  • 16
  • 28
0

This looks ok as is. If you only printed one line in each case you could replace the if/else block with:

System.out.println("\n" + var3 + " is "
                        + (var3 % var2 == 0 ? "" : " not ")
                        + " divisible with " + var2);

But since you need to print 2 lines in one case and only one in the other case your if/else construct is probably better.

assylias
  • 321,522
  • 82
  • 660
  • 783
0

I think the ternary operator can be used for simple one-liners, not for more elaborate code:

System.out.println(var3 % var2 == 0
    ? "\n" + var3 + " is divisible with " + var2 + "\n" + var3 + " divided with " + var2 + " is: " + var3 / var2
    : "\n" + var3 + " is not divisible with " + var2);

Not a lot better is it?

Most solutions offered here strike me as a case of, what I call, 'over-automating'.

0

How about this?

String text1 =  "\n" + var3 + " is divisible with " + var2 + "\n" + var3 + " divided with " + var2 + " is: " + var3 / var2;
String text2 =  "\n" + var3 + " is not divisible with " + var2;   
String res = var3 % var2 == 0 ? text1 : text2;
    System.out.println(res);
ionutioio
  • 218
  • 1
  • 6