Droidman
Droidman

Reputation: 11608

java - use of ternary operator

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));

    }

Upvotes: 3

Views: 1153

Answers (3)

raina77ow
raina77ow

Reputation: 106375

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. )

Upvotes: 3

LaGrandMere
LaGrandMere

Reputation: 10359

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)
                        )
                    )

Upvotes: 1

Marc
Marc

Reputation: 6190

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;

Upvotes: 1

Related Questions