Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

String trim and javaStackOverFlow

Thread view: 
Cruella DeVille - 12 May 2006 21:53 GMT
Hey

I'm trying to create a method that checks input from an array of
JTextField. I thought the trim() method from String automatically was
loaded, but I keep getting the "method not defined in class Form"  (of
type JInternalFrame)

I also thought that the return of JTextField.getText() was a String so
that I could use the trim() method in this, but no.

Some code:

public boolean checkInput(){
        for(JTextField content : textFields){
            if(trim(content.getText()).equals("")){
                return false;
            }
        }
        return true;
    }

    private String trim(String string){
        String s = trim(string);
        return s;
    }

Why am I getting the StackOverFlowException from this code? I'm
obviously missing something here.
Steve W. Jackson - 12 May 2006 22:12 GMT
> Hey
>
[quoted text clipped - 24 lines]
> Why am I getting the StackOverFlowException from this code? I'm
> obviously missing something here.

The trim() method of String is exactly that -- a method that exists on a
String object.  So your trim() method in the example isn't using
String.trim at all.  Instead, the first line of your trim() method calls
itself recursively, as does the next, and the next, and the next, etc.,
until the stack can't take any more.

If you want to keep your trim method, then it should become:

private String trim(String string) {
 return string.trim();
}

But in reality, it's a wasted method call.  Up in your earlier code,
where you pass content.getText() to this method, simply change it to
content.getText().trim() instead and you get the desired result.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Matt Humphrey - 12 May 2006 22:13 GMT
> Hey
>
[quoted text clipped - 24 lines]
> Why am I getting the StackOverFlowException from this code? I'm
> obviously missing something here.

You are missing that you are recursively calling trim () within itself.
This keeps going down over and over without stopping.  You want instead:

String s = string.trim ();

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Cruella DeVille - 12 May 2006 22:20 GMT
Upsi!
In php, in which I program the most, the syntax is trim(the_string)

Thanks alot!
Eric Sosman - 12 May 2006 22:23 GMT
Cruella DeVille wrote On 05/12/06 16:53,:
> Hey
>
[quoted text clipped - 24 lines]
> Why am I getting the StackOverFlowException from this code? I'm
> obviously missing something here.

   Because your checkInput method calls your trim method,
which in turn calls your trim method, which in turn calls
your trim method, which in turn ...  "Lather, rinse, repeat."

   Immediate fix: Inside your trim method, replace the first
line with

    String s = string.trim();

   Better fix: Get rid of your trim method altogether, and
in the checkInput method write

    if (content.getText().trim().equals("")) {

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.