aspose file tools*
The moose likes Java in General and the fly likes Using Boolean Value Returned Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Boolean Value Returned" Watch "Using Boolean Value Returned" New topic
Author

Using Boolean Value Returned

Bob Ryan
Greenhorn

Joined: Oct 14, 2004
Posts: 6
Hi All
I have been trying to find the correct syntax to check when I have called a method it has been completed or not.
Say that I call the method equalTo()
public boolean equalTo( int n)
{If(n=10)

return true;
else
return false;
}

I know that I will get a boolean returned either way (true or false). So how do test this for say a valid number input.
Regards
Bob
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Well, thats why we have Exceptions

You can throw some custom exception like InvalidNumberException...

But in your method arg is 'int' type so I wonder how it won't be a valid number? Do you have a specific criteria defined for "valid number" in addition to it being an 'int'. e.g. if the number is between 10 and 100 then its valid, otherwise its considered invalid?

Regards,
Maulin
Bob Ryan
Greenhorn

Joined: Oct 14, 2004
Posts: 6
My main concern is

return true;//Value Matches
else
return false;//Value Doesn't Match
If I called the method from another class I need to compare the returned value. I haven't seen any examples of this in my text
But I did see it in a Assignment Solution !!!

while(!myNum.equalTo(kb.getChar()))
System.out.println("illegal Number: re-enter");

This code may be of use to someone else.
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
It's pretty easy to get mislead by reading code fragments, but the code fragment you show doesn't look like anything that will compile or execute, without seeing the surrounding class. For one thing, most implementations of getChar() return the character value, not the integer value.
Here is a brief but complete program that has been run (on Java 5 aka 1.5 aka Tiger) and works:

For more information see Recipe 10.5 of the Java Cookbook, 2nd Edition, page 262.

Ian
[ October 14, 2004: Message edited by: Ian Darwin ]

Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Bob,

Actually, I wonder if your problem is simply using the assignment equals (n=10) where you actually want the relational equals (n==10).



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Boolean Value Returned