• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing my own length() method  RSS feed

 
Gaurav Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

Today i was writing my own length() method whose functionality is exactly similar to String.length(). My code is below:


I kept thinking on it right from the morning and i tried various approaches like extracting each charecter from string and comparing it. But none of the approaches gave me perfect result.


Now i wanna know is this a right approach to do the same ? Or we should not use the exceptions in our programs and try something else.

Kindly reply.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never use exceptions for loop control; it's quite inefficient, and inelegant, to boot.

Why are you doing this? Is it a class assignment, just a personal learning thing, or what? It's kind of a silly thing to do, frankly.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that you are not allowed to use the length() method from the String class, you are probably not allowed to use the length field from the byte array or the char array that can be returned either. Given these restrictions, taking an exception hit may be the only option.

Regardless, I agree with EFH, this is a pretty silly exercise.

Henry
[ September 03, 2008: Message edited by: Henry Wong ]
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Henry]: Considering that you are not allowed to use the length() method from the String class, you are probably not allowed to use the length field from the byte array or the char array that can be returned either.

Assuming that's the case, I can imagine several other solutions, though they're similarly silly. E.g. counting the number of times you can take str.substring(1) before isEmpty() returns true. Or using an enhanced for loop to go through the char[] array, counting as you go. Regular expressions could also be used in various ways. One might argue that most of these techniques will use the length field implicitly - but then, so would the StringIndexOutOfBoundsException technique, since that's how the String class knows to throw the exception, by checking against length.

My guess is that this is an assignment to try to get the student to catch an exception. But yes, it seems pretty silly.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Simmons:
Or using an enhanced for loop to go through the char[] array, counting as you go.

That's cheating since it still uses the length field of the array - you just don't see it. I've decompiled an enhanced for loop once and it ended up being just the same as a regular for loop, both for arrays and Iterables.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, obviously. Just as the original StringIndexOutOfBoundsException uses the length field to detect the error and throw an appropriate exception. As I've already stated above. Whether you consider any of this "cheating" depends on exactly what rules were given for this extremely silly exercise. There are quite a few solutions that may be possible, depending on the vagaries of this ill-defined problem.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But using the enhanced for loop is just syntactical sugar, unlike the StringIndexOutOfBoundsException.

But yes, it's a silly excersise. In C I'd understand this excersise (since there is an obvious marker for the end of the string), but not in Java.
 
Gaurav Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your inputs.

This was a class assignment in which i can't call any method of String class except charAt().

I can't use the length field because to do so i have to convert this String into an array which will surely use String Class method.

So the conversion into array is only possible by using charAt() method but i can't found the end of line character in java.

I tried another approach and the code for same is below:



The above method worked fine.
If i pass '\u0000' by taking input from keyboard at runtime, it work fine .

But when i pass "Hi \u0000 j" the output must be 6 but due to my logic i get 3. My folks never caught this problem but i know where this method won't work ?



Is there any other solution you guys see to this problem ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tested your code with that String passed as a command line argument, and the result is 11. Therefore, I assume you hard coded that String in your code.

Now inside your code there is also a \u0000. You must know that \ is an escape character in Strings; if followed by \u it will just insert that character in your String. Therefore, at position 4, there already is a \u0000, and your code fails!

Unfortunately, there is nothing you can do about this - it is a valid String character after all, unlike in C.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!