How to return a String in a boolean method

K

Kermit Piper

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
 
S

Steve W. Jackson

"Kermit Piper said:
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.
 
K

Kermit Piper

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,
 
O

Oliver Wong

[post re-ordered]

Kermit Piper said:
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
 
S

sks

Kermit Piper said:
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.

You could throw and exception like IllegalUPCFormatException("Your string is
too long")
 
M

Mark Space

Kermit said:
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");
throw new IllegalArgumentException(
"UPC should be all numeric.");
//return exceptionString; //Type mismatch
//return false;
}
return true;
}
return false;
}
Thanks,
KP

IllegalArgumentException is a type of RuntimeException, which does not
have to be declared in your method prototype.

You can use toString() on the exception after it is thrown to capture
the message string along with all the other exception information, or
use getMessage() to just get the string alone.
 
M

Moiristo

sks said:
You could throw and exception like IllegalUPCFormatException("Your string is
too long")

I've used JSF for my webapp, and I created a class where I could store
validation errors in. So, instead of returning the error, I did:
'ViewUtils.addExceptionMessage("UPC error");'. When rendering the result
page, I could just look whether there were errors, and write them to the
screen if necessary. I think this could easily be implemented in JSP, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top