Results 1 to 6 of 6
  1. #1
    hamster is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

    Default Handy conditional If code to handle "day" and "days". Needs a slight tweak.

    Hi there,

    quick question if anyone knows this offhand - im trying to use the shorthand conditional if statement within a print statement to handle "days" or "day" depending if remainingDays > 1.
    Line 1 is the working code.
    Line 2 is the change im trying to put in (+ (remainingDays > 1) ? " days." : " day.") - the compiler thinks im trying to convert a string.
    Line 3 and 4 is the workaround but is spread across 2 lines but it is possible to use something like line 2.

    Java Code:
    1. System.out.println(minutes + " minutes is  " + years + " years and " + remainingDays + " days.");
    2. System.out.println(minutes + " minutes is  " + years + " years and " + (remainingDays > 1) ? " days." : " day.");
        
    3. System.out.print(remainingDays);
    4. System.out.println((remainingDays > 1) ? " days." : " day.");
    Thanks,
    Hamster.

  2. #2
    hamster is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

    Default Re: Handy conditional If code to handle "day" and "days". Needs a slight tweak.

    Ah spotted it.

    System.out.println(minutes + " minutes is approximately " + years + ((years == 1) ? " year and " : " years and ")
    + remainingDays + ((remainingDays == 1) ? " day." : " days."));

    I wasn't covering the entire condition with brackets.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Handy conditional If code to handle "day" and "days". Needs a slight tweak.

    The following will wrok:

    Java Code:
    System.out.println(
            minutes + " minutes is  " + years + " years and " 
            + remainingDays + ((remainingDays > 1) ? " days." : " day."));
    (Notice how remainingDays is concatenated as well as the conditional day/days string. And how parentheses are used so that the last part is unambiguously a String.)

    But I wouldn't consider it the most readable of code. The one line form - as in your original line 2 - won't survive the CSS transforms of this web site for instance! And breaking the statement over multiple lines doesn't completely restore clarity.

    Consider accepting using multiple statements and/or using printf(). (ChoiceFormat might be overkill in your circumstances - but you should be aware of using externalised resource bundles for strings, and internationalisation.. The aim should be something easily readable and correct ("0 day"?). The code I supplied fails, I'm afraid.

    -----

    [Edit] too slow! :( That's what comes from looking up what ChoiceFormat buys you...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Handy conditional If code to handle "day" and "days". Needs a slight tweak.

    Moved to "New to Java".

    @OP: I don't know how you managed to start this thread in the other forum, but its a completely out of the way place that no-one visits, so I've moved it here.

  5. #5
    hamster is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

    Default Re: Handy conditional If code to handle "day" and "days". Needs a slight tweak.

    I was trying to put it into a more specific sub forum but i guess most of my queries would be suitable in "New Java". Thanks for the feedback on the code!

    Regards,
    Hamster

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Handy conditional If code to handle "day" and "days". Needs a slight tweak.

    No problem. The whole "Learn Java" forum collection is labelled "Attention: Only forum staff can post to these forums!", but that isn't enforced consistently by the forum software and the forums involved are basically unused.

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  2. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 06:40 PM
  3. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •