How to build a class w/ return value?

P

Perfect Reign

I apologize for a newbie question, but I'm just starting with Java.

I'm trying to write a program which includes a class for checking if a
given text field has a value.

My basic logic would run something like:

A button is pressed
On that event, check if a text field has a value
if it does, return true, else return false.

So, I want to say in the action listener event for that button something
like:

if ( checkValue( txtFieldName ) == true)
do something
else
do something else


I'd then have a public funciton for checking the value

public checkValue( String txtFieldName )
{
if ( txtFieldName == "" )
return false;
else
return true;
}

This keeps giving me an error that a return code is needed for a public
class. If i put void in the class, I can run it but then nothing gets
returned.

Now, a bit of background - I've been doing VB for 12 years, so maybe my
thinking is flawed. If so, is there another way of going about this? I've
googled for the past hour and am very frustrated.

--
kai - (e-mail address removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
M

Matt Humphrey

Perfect Reign said:
I apologize for a newbie question, but I'm just starting with Java.

I'm trying to write a program which includes a class for checking if a
given text field has a value.

My basic logic would run something like:

A button is pressed
On that event, check if a text field has a value
if it does, return true, else return false.

So, I want to say in the action listener event for that button something
like:

if ( checkValue( txtFieldName ) == true)
do something
else
do something else


I'd then have a public funciton for checking the value

public checkValue( String txtFieldName )

public boolean checkValue (String txtFieldName)
{
if ( txtFieldName == "" )

if ( txtFieldName.equals(""))
return false;
else
return true;
}
This keeps giving me an error that a return code is needed for a public
class. If i put void in the class, I can run it but then nothing gets
returned.

The compiler wants to know the type of the value you're returning. Methods
that don't return a value specify "void" instead. Also, use .equals to
compare string contents rather than == which only tests object identity
equivalence. The above code also does not test if txtFieldName is null.
Now, a bit of background - I've been doing VB for 12 years, so maybe my
thinking is flawed. If so, is there another way of going about this? I've
googled for the past hour and am very frustrated.

Any basic Java text will show that all methods have a return type or void.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
P

Perfect Reign

This keeps giving me an error that a return code is needed for a public
class. If i put void in the class, I can run it but then nothing gets
returned.

Nvermind - figured it out. Needed to add the type to the class.

public boolean checkValue( String txtFieldName )
{
if ( txtFieldName == "" )
return false;
else
return true;

--
kai - (e-mail address removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
W

Wendy Smoak

Perfect Reign said:
Nvermind - figured it out. Needed to add the type to the class.

public boolean checkValue( String txtFieldName )

Just to be picky... that is a method, not a class.
 
P

Perfect Reign

Just to be picky... that is a method, not a class.

Cool.

I just didn't want to call it a function, which is my tendency. :)


--
kai - (e-mail address removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
A

Andy Flowers

Perfect said:
Nvermind - figured it out. Needed to add the type to the class.

public boolean checkValue( String txtFieldName )
{
if ( txtFieldName == "" )
return false;
else
return true;

Another minor problem. txtFieldName == "" is likely to fail.

Have a look at equals(...)

You would need to use txtFieldName.equals("") along with an initial check to
see if txtFieldName is null

i.e.

if( txtFieldName == null || txtFieldName.equals("") )
{
return false;
}

Take a look at the API documentation etc for further details, but a snippet
from the API definition of equals(...) helps

"Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object
references"
 
J

Joona I Palaste

Nvermind - figured it out. Needed to add the type to the class.
public boolean checkValue( String txtFieldName )
{
if ( txtFieldName == "" )
return false;
else
return true;

That's true, but you can simply do this:

public boolean checkValue(String txtFieldName) {
return txtFieldName != "";
}

or, a better working version:

public boolean checkValue(String txtFieldName) {
return !"".equals(txtFieldName);
}
 
D

Dimitri Maziuk

Joona I Palaste sez:
That's true, but you can simply do this:

public boolean checkValue(String txtFieldName) {
return txtFieldName != "";
}

or, a better working version:

public boolean checkValue(String txtFieldName) {
return !"".equals(txtFieldName);
}

Heh. How about
return ! (txt == null || txt.trim().length() < 1);

Dima
 
T

Tony Morris

public boolean checkValue( String txtFieldName )
That's true, but you can simply do this:

public boolean checkValue(String txtFieldName) {
return txtFieldName != "";
}

or, a better working version:

public boolean checkValue(String txtFieldName) {
return !"".equals(txtFieldName);
}

ew ew yuk!

txtFieldName != null && txtFieldName.length() != 0;
Please don't make me justify why this is better form (to which I respond
"what has happened to education systems?").

--
Tony Morris

JTiger Unit Test Framework for J2SE 1.5
http://www.jtiger.org/
Java Q&A (FAQ, Trivia)
http://qa.jtiger.org/
http://xdweb.net/~dibblego/
 
D

Dimitri Maziuk

Tony Morris sez:
....
ew ew yuk!

txtFieldName != null && txtFieldName.length() != 0;
Please don't make me justify why this is better form (to which I respond
"what has happened to education systems?").

Actually, "".equals() is not so bad if you use it a lot:
"" will be interned, so the more you use this form, the
less overhead you incur. Single call to equals() may well
be more efficient than two comparisons and a call to length()
(plus you may want to trim() before you measure length, that's
probably expensive).

Dima
 

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,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top