• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with a bet : getting it down to one line. (fun)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this is a bit silly , but I am in a bet with a co-worker to get some code down to as few lines as possible.
Basically, this method is a simple one to return the string text for a drop down of minutes.
Here is what I have it at now:

I think I have him beat already, as I don't know if he even knows about the "variable = (condition) ? res1 : res2", but I really want to rub it in. What is stopping me at this point is getting that for loop into one line with the other two.
Any ideas?
Edited the HTML out of the first line.
[This message has been edited by Scott Hiett (edited July 06, 2001).]

[This message has been edited by Jim Yingst (edited July 09, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, but a Noble attempt .
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited your post to put spaces inside each of the HTML tags you were printing - this makes it possible for the tags to be displayed to see, rather than interpreted by the browser. Just delete the spaces from the real code...
To further condense the code, you can factor out the stuff that's repeated in the conditional expression:
<code><pre>
public static String buildMinute(String name, int minute) {
String html = "< select>";
for (int i = 0; i < 61 ; i+=5)<br /> html += "\n<font color="red">< option value=\"" + i + "\"" +</font> ((i == minute) ? " SELECTED>" : ">")
+ ((i < 10) ? "0" : "") + i + "< /option>";
return html += "\n< /select>\n";
}
</pre></code>
For performance, I would replace the String with a StringBuffer, but that would make the code slightly longer. If you really want it all in one line, there's no reason you can't just omit all those pesky newlines, and many of the spaces - Java doesn't need them.
<code><pre> public static String buildMinute(String name,int minute){String html="< select>";for(int i=0;i<61;i+=5)html+="\n< option value=\""+i+"\""+((i==minute)?" SELECTED>":">")+((i < 10)?"0":"")+i+"< /option>";return html+="\n< /select>\n";}</pre></code>
Of course I don't advocate this in general, but if you really want a one-liner...

[This message has been edited by Jim Yingst (edited July 09, 2001).]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, get rid of the loop -- unless you're worried that the number of minutes in an hour might change in the near future. Would give better performance too.
Run it once, take the output and you'll end up with:
<code>
public static String buildMinute(String name, int minute) {
  return "HTML code here.. <option> etc... <script> document.myForm.mySelect[" + minute + "].selected = true; </script> rest o' html";
}
</code>

Or you could use the ternary operator around all sixty concat options. Regardless, not much point in looping if you know the outcome and know that it cannot change (since -O does nothing, you're loop won't get unrolled unless HotSpot feels like it.)
[This message has been edited by Yuri Gadow (edited July 09, 2001).]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about getting rid of for loop using recursion? But this needs change in method signature
reply
    Bookmark Topic Watch Topic
  • New Topic