String trim and javaStackOverFlow

C

Cruella DeVille

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.
 
S

Steve W. Jackson

Cruella DeVille said:
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.

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 =
 
M

Matt Humphrey

Cruella DeVille said:
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.

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 (e-mail address removed) http://www.iviz.com/
 
C

Cruella DeVille

Upsi!
In php, in which I program the most, the syntax is trim(the_string)

Thanks alot!
 
E

Eric Sosman

Cruella DeVille wrote On 05/12/06 16:53,:
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.

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("")) {
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top