Results 1 to 6 of 6
- 04-29-2012, 01:31 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 14
- Rep Power
- 0
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.
Thanks,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.");
Hamster.
- 04-29-2012, 01:47 AM #2
Member
- Join Date
- Apr 2012
- Posts
- 14
- Rep Power
- 0
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.
- 04-29-2012, 01:52 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Handy conditional If code to handle "day" and "days". Needs a slight tweak.
The following will wrok:
(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.)Java Code:System.out.println( minutes + " minutes is " + years + " years and " + remainingDays + ((remainingDays > 1) ? " days." : " day."));
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...
- 04-29-2012, 01:59 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-29-2012, 04:05 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 14
- Rep Power
- 0
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
- 04-29-2012, 07:18 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
Program skips "If" code and goes straight to "Else"
By Logik22 in forum New To JavaReplies: 12Last Post: 01-21-2012, 05:40 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks