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: 1865
posted
0
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?
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 !!!
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 ]