3

I got quite a large code with 4 different conditions which I tried to shorten using the conditional ternary operator as descibed here. However, I can't manage the right syntax since I have more than 2 conditions. Could someone explain how to use the ternary operator in such case? My code goes below

And no, I'm not asking to write code for me, I'm looking for an explanation of ternary operator use with multiple conditions

     if (mp.getCurrentPosition() / 1000 / 60 < 10
            && mp.getCurrentPosition() / 1000 % 60 < 10) {
        tvTimeElapsed.setText("0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":" + "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    } else if (mp.getCurrentPosition() / 1000 / 60 < 10
            && mp.getCurrentPosition() / 1000 % 60 >= 10) {

        tvTimeElapsed.setText("0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    } else if (mp.getCurrentPosition() / 1000 / 60 >= 10
            && mp.getCurrentPosition() / 1000 % 60 < 10) {

        tvTimeElapsed
                .setText(Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                        + ":"
                        + "0"
                        + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    } else {

        tvTimeElapsed
                .setText(Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                        + ":"
                        + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    }
6
  • You'll simply have to nest several conditional operations in each other. But that's ugly, and hard to read. So don't. =) Commented Mar 20, 2013 at 11:42
  • 2
    The usual advice is: don't! Trying to embed multiple conditions with multiple nested ?: just results in code that's hard to read. Commented Mar 20, 2013 at 11:42
  • 1
    I would question why you're trying to shorten it. Short code isn't always better. As it stands, the code you've posted is quite complicated anyway, so IMO refactoring it to use ternary operators would make it incredibly unreadable Commented Mar 20, 2013 at 11:43
  • well my class is already large and implements serveral interfaces, so I thought shortening the code whenever possible would be a good practice Commented Mar 20, 2013 at 11:44
  • I agree with the previous comments about readability. But for your understanding, you can nest ternary operators: a ? b : (c ? d : (e ? f : g)) Commented Mar 20, 2013 at 11:44

3 Answers 3

3

How about just this, without any ternaries at all:

int seconds = mp.getCurrentPosition() / 1000;
tvTimeElapsed.setText(
    String.format("%02d:%02d", seconds / 60, seconds % 60);
);

You don't need to reinvent the wheel with all these conditions here: there's an internal Java string formatter for all these hard choices and stuff. )

Sign up to request clarification or add additional context in comments.

1 Comment

shame on me I haven't thought of that. Thanks m8
1

Removed the first part

Edit:

int var1 = mp.getCurrentPosition() / 1000 / 60;
int var2 = mp.getCurrentPosition() / 1000 % 60;

String hour = var1 < 10 ? "0" + var1 : var1;
String minute = var1 < 10 ? "0" + var2 : var2;

String complete = hour + ":" + minute;

Comments

1

I must agree with all comments: it's ugly.

String textToSet = (mp.getCurrentPosition() / 1000 / 60 < 10 ? 
( mp.getCurrentPosition() / 1000 % 60 < 10 ? "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":" + "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60) : "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60)) : 
( mp.getCurrentPosition() / 1000 % 60 < 10 ? "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60) : Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                        + ":"
                        + "0"
                        + Integer.toString(mp.getCurrentPosition() / 1000 % 60)) )

With replacement of the Integer :

Integer int1 = mp.getCurrentPosition() / 1000 / 60;
Integer int2 = mp.getCurrentPosition() / 1000 % 60;

tvTimeElapsedText = (int1< 10 ? 
                        (int2 < 10 ? 
                            "0" + Integer.toString(int1) + ":" + "0" + Integer.toString(int2) :
                            "0" + Integer.toString(int1) + ":" + Integer.toString(int2)
                        ) :
                        (int2 < 10 ? 
                            Integer.toString(int1) + ":" + "0" + Integer.toString(int2) :
                            Integer.toString(int1) + ":" + Integer.toString(int2)
                        )
                    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.