This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes get the length of the string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get the length of the string" Watch "get the length of the string" New topic
Author

get the length of the string

johny Sill
Greenhorn

Joined: Jul 27, 2006
Posts: 1
Hi,

How to get length of the string with out using string length method,

Help me,

Thanks in advance
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"johny beginer",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted, often without warning

thanks,
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You're going to need to give us a good reason not to use the length method, that's what it is for.
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38


Fahd Shariff<br />"Let the code do the talking"
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Print it, cut it out, weigh it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Stan James:
Print it, cut it out, weigh it.


Thats how my chemistry professors used to calculate the area under a curve back when they were undergrads. Gotta love the old - skool.
[ July 28, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
This can be done in one method call without using length() and without printing anything out. Look at the documentation and check out detailed documentation for each method that returns an int. There's at least one in particular that will return an index equal to this.length(). Other's could easily return something that will not be a String but will easily tell you the length of the original String.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
My dad gave me that, and I got credit in HS science calculating the area of an irregular shape. It's right up there with ways to determine the height of a building using a barometer.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Stan James:
My dad gave me that, and I got credit in HS science calculating the area of an irregular shape. It's right up there with ways to determine the height of a building using a barometer.


From this parible/ article:
  • Take a barometer to the top of the building, attach a long rope to it, lower the barometer to the street and then bring it up, measuring the length of the rope. The length of the rope is the height of the building.
  • Take the barometer to the top of the building and lean over the edge of the roof. Drop that barometer, timing its fall with a stopwatch. Then using the formula S = �at�, calculate the height of the building.
  • ...take the barometer out on a sunny day and measure the height of the barometer and the length of its shadow, and the length of the shadow of the building and by the use of a simple proportion, determine the height of the building.
  • ...take the barometer and begin to walk up the stairs. As you climb the stairs, you mark off the length of the barometer along the wall. You then count the number of marks, and this will give you the height of the building in barometer units. A very direct method.
  • ...tie the barometer to the end of a string, swing it as a pendulum, and determine the value of 'g' at the street level and at the top of the building. From the difference of the two values of `g' the height of the building can be calculated.
  • take the barometer to the basement and knock on the superintendent's door. When the superintendent answers, you speak to him as follows: "Mr. Superintendent, here I have a fine barometer. If you tell me the height of this building, I will give you this barometer."
  • Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    [GR]: Take a barometer to the top of the building, attach a long rope to it, lower the barometer to the street and then bring it up, measuring the length of the rope. The length of the rope is the height of the building.

    Yeah, that barometer is really irreplaceable in this case. That role couldn't possibly be filled by al large rock. Or a small rock. Or... hell, no rock or anything else at all. I think ropes have enough weight of their own to hand in a generally downward direction.

    [GR]: ...take the barometer and begin to walk up the stairs. As you climb the stairs, you mark off the length of the barometer along the wall. You then count the number of marks, and this will give you the height of the building in barometer units. A very direct method.

    Yep. I assume you meant the veritical length, as the length along the general direction of the staircase is irrelevant. You can also do this more directlly by having someone scale the building, if the edifice is suitably craggy for footholds. If you're not moving straight up (or down) - are you drawing a really precise horizontal line every time you need to delimit two different barometer height measurements? Probably not. Seems like this will be a rather large source of errors for this method.

    [GR]: ...tie the barometer to the end of a string, swing it as a pendulum, and determine the value of 'g' at the street level and at the top of the building. From the difference of the two values of `g' the height of the building can be calculated.

    the funny thing about this answer is that it overlooks that it's achieved a better answer about halfway through, before anyone has to travel up (or down) the length of the building. (Easy in an elevator, but pretty tiring if you have to use the staircase.) I'm thinking that the variation in g is a very poor way to determine building height, considering that local variations in density (e.g.. mountains) are likely to be much more significant than differences in distance from the center of the earth is. Feh. Maybe this is different for those of you in flat areas of the Earth, but for me: forget about the difference between 'g' values at the bottom and top of the building - just use a standard value for g-at-anywhere-roughly-on-the-surface-of-the-earth, plus the period of the pendulum (at top or bottom; doesn't really matter), and plug into the pendulum equation to determine the length of the pendulum. Assuming the earlier just-measure-the-length-of-the-damn-rope technique has failed for some reason - don't want to overlook the even-more-obvious solution.

    [GR]: take the barometer to the basement and knock on the superintendent's door. When the superintendent answers, you speak to him as follows: "Mr. Superintendent, here I have a fine barometer. If you tell me the height of this building, I will give you this barometer."

    While this is my favorite answer to this particular chestnut, I have to think that there's a significant possibility the super has no idea what the height of the building is, but is perfectly willing to lie on the spot in order to obtain that fine barometer. (Tremendously valuable, no doubt.) Trust no one.


    "I'm not back." - Bill Harding, Twister
     
    Don't get me started about those stupid light bulbs.
     
    subject: get the length of the string
     
    Similar Threads
    HOWTO check for click of Cancel button
    amount of digits entered(without a tokenizer)....
    StringOutOfBoundsException
    Array of Strings
    maximum length of String = 32k ?