- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
public class WTFGenerator {
public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]);
} catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits <= 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i < max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }
// if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad < 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i <= numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad > 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i <= digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }
}//end class
Volmarias, I think this is a bit sad, doing all this function just for generating a copy&paste code :)
Admin
args[1] is the second argument
Admin
uh? half of my code has disappeared! Damn the < tag!
public class WTFGenerator { public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]); } catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits <= 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i < max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }
// if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad < 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i <= numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad > 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i <= digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }
}//end class
Admin
Oppps, I was thinking in bash, where $0 is the script name in the command line, and $1 is the first argument...
Admin
hum, what about the pre tag? It works, doesn't it?
Admin
thisForumSoftwareHaters++;
Admin
in ansi sql:
select substr(to_char(to_number(:sequenceCode)+1),'099'),1,2) from ...
Admin
Wouldn't be possible, since even the frigging page number is transferred via POST instead of GET (which means that you can't reload the page without getting some retarded "hey mate I have to resend informations to the server, are you ok with that?" popup from your browser...
While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.
AND you can leave the generator somewhere in case you need to "extend" the feature/code !
Admin
[blockquote] [pre] public class WTFGenerator {
public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]);
} catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits LESS OR EQUAL TO 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i LESS OR EQUAL TO max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }
// if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad LESS THAN 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i LESS OR EQUAL TO numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad GREATER THAN 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i LESS OR EQUAL TO digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }
}//end class
[/pre] [/blockquote]
( sorry for filling the forum with horribly formatted posts )
Admin
I guess you wanted
BTW you're not outputting "ELSE IF", but only "IF"s
Admin
I use excel or Ultraedit to generate my copy/paste code...
Admin
I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.
http://www.tldp.org/LDP/abs/html/comparison-ops.html
Admin
Yes, that's it. Can you tell how you did it?
Admin
Admin
<FONT face="Courier New" size=2>see, nobody has the big question here. why the hell is this function needed? in what context is it used? HOW DID IT GET THERE GOSH DARN IT.</FONT>
Admin
<FONT face="Courier New" size=2>brillant.</FONT>
Admin
WTF... like a Pentium doing divisions... fast, but wrong.
The right way to do it:
Admin
Yes. You have to create a post, then immediatly switch to HTML view (clic on the HTML tab at the bottom), and use
The important thing is to remember switching to HTML right out of the bat, or you'll get a much fubared code when you will.
Admin
Gosh, that piece of crap managed to interpret my l33t escaped characters... let's try again
edit: leet, can't even write HTML entities, that sorry excuse for an editor creates HTML from them...
[blockquote][pre]your_code[/pre][/blockquote]
Replace [ and ] by lt and gts.
Admin
"I can confirm that it is also gone for registered users. "If you can't fix it, kill it"."
cough
This may be good news - the author of the code may be realising that there are problems, and maybe coming to the conclusion that they need fixing. I hope so.
Or perhaps Alex disabled the button.
Admin
The HTML tab, which, of course, <b>doesn't appear on Opera, so I have to swith to IE</b>. ouch!
Admin
The HTML tab, which, of course, doesn't appear on Opera, so I have to swith to IE. ouch!
Admin
An all-too-true statement I read in some book (IIRC "Refactoring: Improving the Design of Existing Code"):
"There is very little that is less logical than so called 'business logic'"
Usually it's a huge bunch of special cases that ultimately exist because of marketing (such as allowing salesmen to offer "special conditions" to convince uncertain customers).
Admin
Well, it's not *THAT* bad, I can't even get the frigging captcha to appear with Firefox, which mean that I can't even post if i'm not using MSIE (me thinks i'm going to create an account just for that, because it's getting more and more annoying)
Admin
WTF?
<font size="4">$ wtf="hello world, i can count"
$ echo ${#wtf}</font>
RTFM! LOL, LMAO
Admin
I occasionally leverage my foot into someone's posterior.
Admin
nice to know! guess you are right with that RTFM...
Admin
Well, now we're getting into my mantra, "What are the requirements?".
The original solution (the original WTF) was able to gracefully handle any input that did not conform to a two digit number and return the input intact. Is that the requirement? I can't tell from the info given. All we really have is the de facto results of the code as given.
It is, to me, better to validate the data and only operate on valid inputs and return the character string.
Admin
Hahahaha
I hope at least _one_ person tried this...
Lol
-dave-
Admin
++thisForumSoftwareHaters++
Count me in on this as well...
-dave-
Admin
Ah, the biggest WTF of all. Still, it could be worse - it could use session data to keep track of what page you're viewing (shudder)
Admin
<FONT face="Courier New">Re: guess you are right with that RTFM</FONT>
<FONT face="Courier New">In principle, yes, but the man page of bash is such a mess, you can't really blame anyone for missing 'obvious' things like getting the number of characters in a variable, or parameter in sh-speak. You have your positional parameters, parameter expansion, brace expansion, tilde expansion, arithmetic expansion and whatnot. This thing ${#var} is buried in parameter expansion. :-S Of course you have to find all this out in that crappy 'less' pager. </FONT>
Admin
Admin
BZZZZZZZZZZZZZZZZT! Thank you for playing, but 99 becomes 0 in the original code, and values above 99 remain unchanged.
I don't know this language, but in C++, it would be
if (sequenceCode < 100)
sequenceCode = (sequenceCode + 1) % 100
to duplicate the exact functionality of the original.
Admin
Not exactly, since sequenceCode is a string and all values except "00", "01", ..., "99" stay unchanged.
Admin
Lovely. I guess '00' stays '00'. Glory!
ML
Admin
WTF, you are right!!!
Admin
"00 stays 00" -- not a special case, that's the bug in the original code that had to be fixed.
Admin
Doesn't anyone see it?
This was written by a SENIOR programmer.
That guy first learned to code in COBOL, and his minions still suffer the consequences.
Admin
That's an extremely proactive definitization of leveraging the word leverage in bizTalk
How long did it take you to definitize you reply?
Admin
<FONT face="Courier New" size=2>SET @sequenceCode =
CASE
WHEN LEN(@sequenceCode) <> 2 OR ISNUMERIC(@sequenceCode) <> 1 THEN @sequenceCode
WHEN @sequenceCode IN ('00', '99') THEN '00'
ELSE REPLACE(STR(CONVERT(int, @sequenceCode) + 1, 2, 0), ' ', '0')
END</FONT>
Admin
Great, I just learned there's a difference between programming and writing stored procedures. Way to shift my paradigm!
Admin
Test
Admin
Seeing as how everyone else has taken a shot at this, I thought I would throw in my own code. It will leave the value the same if it is not numeric, less than 1 or greater than 99. It will make the value "00" if it is equal to 99. Otherwise, it will increment the value by 1 and will also pad with zeros if the return value is less than 10.
-- first - validate that the input is actually a number
if isnumeric(@sequenceCode) = 1 --note, cannot leave off the "= 1" as T-SQL will not accept "if isnumeric(@sequenceCode)" as a valid boolean expression
begin
-- make a temp variable so we don't have to keep casting
declare @sequenceCodeAsInt int
set @sequenceCodeAsInt = cast(@sequenceCode as int)
-- check value is in range (greater than zero and less than 100)
if @sequenceCodeAsInt > 0 and @sequenceCodeAsInt < 100
begin
--increment value by one, cast to varchar and choose last two digits (which will be '00' for 100)
set @sequenceCode = right(cast(@sequenceCodeAsInt + 1 as varchar),2)
--left pad with '0' if length is less than 2
set @sequenceCode = replicate('0',2 - len(@sequenceCode)) + @sequenceCode
end
end
Admin
rofl[pi]
Admin
you could also use:
$ length=`expr length $wtf`
but if the goal is to pad the number, why not just use
$ printf "%.2d" $x
$ help printf
Cheers,
Admin
I'm a few years late on this, but how about this?:
This code also handles the '00' case -- the original sequence of 'IF' statements didn't contain a case for it, so its value should remain unchanged. :-)