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