Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Boolean Value Returned

 
Bob Ryan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic