How to build a class w/ return value?  
Author Message
Perfect Reign





PostPosted: 2005-4-7 1:27:00 Top

java-programmer, How to build a class w/ return value? 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 - email***@***.com - 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".
 
Matt Humphrey





PostPosted: 2005-4-7 1:46:00 Top

java-programmer >> How to build a class w/ return value?
"Perfect Reign" <email***@***.com> wrote in message
news:email***@***.com...
> 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 email***@***.com http://www.iviz.com/


 
Perfect Reign





PostPosted: 2005-4-7 1:49:00 Top

java-programmer >> How to build a class w/ return value? On Wed, 6 Apr 2005 10:26:56 -0700, someone posing as Perfect Reign was
again brilliant and wrote:

> 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 - email***@***.com - 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".
 
 
Wendy Smoak





PostPosted: 2005-4-7 1:56:00 Top

java-programmer >> How to build a class w/ return value? "Perfect Reign" <email***@***.com> wrote:

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

--
Wendy



 
 
Perfect Reign





PostPosted: 2005-4-7 5:14:00 Top

java-programmer >> How to build a class w/ return value? On Wed, 6 Apr 2005 10:56:08 -0700, someone posing as Wendy Smoak donned
fireproof bloomers and chiseled in the wall:

> "Perfect Reign" <email***@***.com> wrote:
>
>> 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.

Cool.

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


--
kai - email***@***.com - 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".
 
 
Andy Flowers





PostPosted: 2005-4-7 5:22:00 Top

java-programmer >> How to build a class w/ return value? Perfect Reign wrote:
> On Wed, 6 Apr 2005 10:26:56 -0700, someone posing as Perfect Reign was
> again brilliant and wrote:
>
>> 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;

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"




 
 
Joona I Palaste





PostPosted: 2005-4-10 21:38:00 Top

java-programmer >> How to build a class w/ return value? Perfect Reign <email***@***.com> scribbled the following:
> On Wed, 6 Apr 2005 10:26:56 -0700, someone posing as Perfect Reign was
> again brilliant and wrote:

>> 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;

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);
}

--
/-- Joona Palaste (email***@***.com) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
 
 
Dimitri Maziuk





PostPosted: 2005-4-11 1:16:00 Top

java-programmer >> How to build a class w/ return value? Joona I Palaste sez:
> Perfect Reign <email***@***.com> scribbled the following:
>> On Wed, 6 Apr 2005 10:26:56 -0700, someone posing as Perfect Reign was
>> again brilliant and wrote:
>
>>> 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;
>
> 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
--
... If you want to make sure you don't put a Pig in a List of airplanes and
have it fail at insertion rather than extraction, use
planelist.add((Airplane)o) instead of planelist.add(o). It's that easy.
-- Mark 'Kamikaze' Hughes
 
 
Tony Morris





PostPosted: 2005-4-11 5:49:00 Top

java-programmer >> How to build a class w/ return value? > > 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);
> }

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/


 
 
Dimitri Maziuk





PostPosted: 2005-4-12 5:12:00 Top

java-programmer >> How to build a class w/ return value? 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
--
Sufficiently advanced incompetence is indistinguishable from malice.