File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes Machiavellian ways to get a String's length without using length() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Machiavellian ways to get a String Watch "Machiavellian ways to get a String New topic
Author

Machiavellian ways to get a String's length without using length()

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5

Seems to me that would exclude bitLength()


While you were writing the post, I thought about this and made a variation without bitLength.

[Edit by Winston: Split from here as we were getting into the realms of fantasy. Great fun though; and any further contributions happily received. ]


Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5



luck, db
There are no new questions, but there may be new answers.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Like it. My offering:
Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
To replace all characters with a digit, and then to produce a numeric value, and to tell the length on an arithmetic basis was an idea
which we elaborated many varations for.

Let us see a solution which needs only the given string: it does not produce an auxiliary one.


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Here's another super horrible ugly solution.

Do not ever use this for real. I'll shame you on TheDailyWTF.com if you do.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jesper de Jong wrote:Here's another super horrible ugly solution.

Hate to burst your bubble, but the original post did say "without using any built in length methods or length properties".

I appreciate its dastardly approach though; particularly the UTF-16 and '/ 2'.

Winston
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

My take:

Magic number needs to be determined experimentally for given platform, string length and JVM settings.

Not very reliable, but definitely Machavellian
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Ahem. "Machiavellian"
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Mike Simmons wrote:Ahem. "Machiavellian"

I was surprised too, but "Machavellian" passed the Wikipedia test: http://en.wikipedia.org/wiki/Machavellian
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I'm not sure what that's supposed to signify - looks like a redirect to the proper spelling. No indication that Machavelli is actually correct in any way.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

You're probably right. I didn't expect Wikipedia to contain redirects from an incorrect spelling to a correct one and though that "Machavellian" is used in some language or region. In any case, "Machavellian" gets a lot of hits on Google, so it looks like this incorrect form is in a quite widespread use.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Mike Simmons wrote:Ahem. "Machiavellian"

Ah. My fault. It was misspelt in the subject too. Corrected.

Well spotted.

Winston
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
The word comes from the name Niccolò Machiavelli so there is not much freedom how to spell it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Winston Gutkowski wrote:
Jesper de Jong wrote:Here's another super horrible ugly solution.

Hate to burst your bubble, but the original post did say "without using any built in length methods or length properties".

I appreciate its dastardly approach though; particularly the UTF-16 and '/ 2'.

I was disappointed to see Jesper's contribution above, if only because it was essentially the same as the idea I came up with, and days before I came along. However, I offer improvements: One, a way to avoid the "built in length methods or length properties" (as much as any of these do, anyway). And two, a minor bug fix: UTF-16 allows an optional byte order mark, which can give you an off by one error - it did show up on my Mac, but not on my Linux box. This is eliminated by using UTF-16LE or UTF-16BE instead. Oh, there's also three: this method should be considerably more performant, avoiding file I/O.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I also question why it was necessary to split this off from the original thread - anything other than simply using length() is a silly waste of time, in a professional setting. The whole premise is really fodder for Programming Diversions.

I also note that almost all these methods also use length internally - we're just delegating the "dirty" work to other standard libraries. The only potential exception I see is Martin's contribution - if we eliminate the "Multiply it 4096 times" loop, it actually does seem to not rely on any internal length properties. Of course it then becomes very imprecise. But for a long enough string, it can at least give an approximation. Kudos for a truly original idea there.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Mike Simmons wrote:I also question why it was necessary to split this off from the original thread...

Maybe overkill on my part; I'm still relatively new to this moderating business. It just seemed to me that we'd got the message across to OP that not using length() wasn't a great idea, and that the rest was more suitable as a fun thread for the PD forum.

Winston
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
> as a fun thread

It has become one indeed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Machiavellian ways to get a String's length without using length()