Please do not post the same question in multiple forums. It causes duplication of effort as the community tries to help everyone.
CarefullyChooseOneForum Also, check your private messages for an important administrative issue.
I have an idea that may work, but it depends exactly on what the constraints on the answer are. Is this a homework question that the user is supposed to solve? Exactly how is the question worded so I know if my answer is applicable.
Reflection doesn't count, that's still querying the property. toCharArray().length also doesn't count because that's calling the property indirectly. As is toString().length(). Adding the String to a StringBuilder calls length() on the String, so again no-go.
Wouter Oet wrote:With "normal" Java you can't. But with reflection you can:
But then again, why would you want this?
I hate to burst your bubble but the char value can be larger than the actual String. When substring is called it the substring will share the same char. You can see this by declaring s as "asdf".substring(2). It's the count field that you need.
Ah right. I never think of these nasty ways. Using exceptions for control flow is just bad. I still hate Sun for not giving us a way for testing if a String can be parsed into an int / long / ... without actually trying the parsing and catching NumberFormatException.
Joined: Aug 05, 2005
Rob Prime wrote:Using exceptions for control flow is just bad.
I agree, but when you get these sort of requirements, sometimes you have to turn to the dark side.
Joined: Dec 12, 2010
Dear all, This was a question asked at the Interview. I tried converting the string into array, StringBuffer. But was of no use.
And regarding post by Joanne Neal, we can loop using charAt until we get OutofBound exception. But it is a runtime exception.
Is it a good practice to catch the Runtime Exceptions and handle them?
It would still be the length of the original String. indexOf would return the index of the A character which is at the end of the original String. Therefore, the index of the A in the modified String is equal to the size of the original String.
Joined: Dec 12, 2010
@Jan Hoppmann, really nice idea. Thanks a lot for the solution. Thanks to all who posted on this thread. Thanks for your time and interest in solving this problem.
You do understand that all of these solutions are purely academic? There is one, and only one, solution you should ever use in production code: call the length() method.
Joined: Dec 12, 2010
Yes Rob, in the production the best practice is to use available APIs. But still these kind of cheeky questions will arise in the interviews which are unavoidable. Anyhow, I even tried checking the actual API implementation of length() method which just returns private count of String object.
My first thought was to convert the string to StringBuilder or to char array and find the length. Even then to traverse the array till last element we need length!. So dropped them. At one time I thought of appending some marker character to the end of StringBuilder or array and manipulating. Still, what if the same marker character used in the program is the last character in the given string! Doing the case conversion and then appending the marker character never flashed in my mind.
Anyways, I have found myself a gem by finding coderanch
Rob Prime wrote:It would still be the length of the original String. indexOf would return the index of the A character which is at the end of the original String. Therefore, the index of the A in the modified String is equal to the size of the original String.
1) check whether the given string equals("") and if yes, it is of zero length.
Otherwise let n := 1
2) Make a cycle listing all possible strings of the length "n"
and check whether the given string equals to one of then.
If yes, it is of length "n"
Otherwise let n := n + 1 and go to 2)
This "solution" not only avoids the usage of length() on strings, stringbuffers etc. but it is also very inefficient.,
which contributes to the fun. Fun is the major (if not only) benefit to be hoped from the exercise.
Ivan Jozsef Balazs wrote:This "solution" not only avoids the usage of length() on strings, stringbuffers etc. but it is also very inefficient...
Actually, not as bad as you might think, because you can re-use the code to create a brute-force password cracker. Of course, it might run for years - literally - but what's a few eons to a computer?
BTW, you guys do realise that this question was from Jan. 2011, so I suspect Elvis has left the building.
Isn't it funny how there's always time and money enough to do it WRONG?
Ivan Jozsef Balazs
Joined: May 22, 2012
One could of course devise a version with a probable solution still slower.
0) Grab a really good random generator.
pick randomly a "k" value between 0 and n.
pick randomly a string of the length "k"
compare it against the original one and if they are equal, the string of the length k,
otherwise repeat this very step.
No w I say if the random generator is really good, then with probability 1 you get the length with the above random algo.
Otherwise we had an infinite chain of wrong guesses.
First we see that the strings length will be picked almost surely (that is, with probability 1) in an infinite number of times.
Well, given for any R (>r) the probability that the right number "r" is not guessed any longer after step "R" is as follows.
At step R+1 we can pick R choices of R+1 to avoid the right choice, that is, R/(R+1)
At step R+2 we can pick R+1 choices of R+2 to avoid the right choice, that is, (R+1)/(R+2)
At step N it is (N-1) / N
That is, if the events are independent, then the cumulated probability is the product which telescopes to R/N.
With R being fixed and N tending to infinity, this tend to zero.
So with probability 1 we guessed the right length in an infinite times.
Now the probability of misguessing the given string from all the ones with this length is a fixed number near to 1 but below it.
And a given number (below 1) raised to the n-th power tends to zero.