Re: How to return a String in a boolean method
[post re-ordered]
"Kermit Piper" <dice_response@hotmail.com> wrote in message
news:1154981456.781488.152760@m73g2000cwd.googlegroups.com...
Steve W. Jackson wrote:
In article <1154979710.253602.260860@h48g2000cwc.googlegroups.com>,
"Kermit Piper" <dice_response@hotmail.com> wrote:
Hello,
I am trying to output a String that I can pass to another method I
already have set-up that will take this value as an input parameter.
But, I keep getting Type mismatch: cannot convert String ot boolean. I
know it's probably simple but I can't see it. If someone could help I'd
sure appreciate it. Here is the method and a comment by the line where
I'm trying to return a String value:
public static boolean checkUPCFormat4(String temp)
{
if (temp.length() >3 && temp.length()<= 4)
{
if (temp.equalsIgnoreCase("0000"))
{
System.out.println("You have entered an invalid 4-digit UPC Code.
Verify and enter ALL numbers.");
String exceptionString
= new String("UPC should be all Numeric");
return exceptionString; //Type mismatch
return false;
}
return true;
}
return false;
}
Thanks,
KP
There are several flaws in this code.
As to the specific question, your method is defined as return a boolean.
So the offending line is simply illegal -- you cannot return a String
object in a method whose declaration says it returns boolean. Can't be
done.
Maybe you should be more explicit about what it is you need to do in
order to get better feedback on how to do it right.
Hello,
What I probably need to do is throw an exception I guess. All I'm
trying to do is simply return a String value I can pass to another
method which will output to a jsp. I'm not sure how much more detail I
can use to explain it.
Thanks,
KP
AFAIK, this is the first time you mentioned "JSP", so that's already a
good start towards providing more information to a thorough solution. Read
http://riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions_20on_20Newsgroups
If you just want to learn about exceptions, see
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
If you want some design advice, the problem with your current method is
that it's doing two different, but related things: It is determining whether
or not there is a problem, and if there is, it is generating an error
message explaining that problem. To fix it, you first have to determine
whether getting incorrect UPCs are a "normal" activity, or an "exceptional"
one. If it's normal, then just have your method check whether the UPC is
valid or not, and in your calling code, check the validity, and display an
error if it isn't valid, or continue normal processing if it is valid.
If the invalid UPC is exceptional, then forget about the checking
method; just proceed as if it were normal, and if you later on find out that
it wasn't valid, throw an exception.
- Oliver