Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

How to return a String in a boolean method

Thread view: 
Kermit Piper - 07 Aug 2006 20:41 GMT
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
Steve W. Jackson - 07 Aug 2006 20:58 GMT
> Hello,
> I am trying to output a String that I can pass to another method I
[quoted text clipped - 23 lines]
> 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.
Signature

Steve W. Jackson
Montgomery, Alabama

Kermit Piper - 07 Aug 2006 21:10 GMT
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

> > Hello,
> > I am trying to output a String that I can pass to another method I
[quoted text clipped - 33 lines]
> 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.
Oliver Wong - 07 Aug 2006 21:39 GMT
[post re-ordered]

>> > Hello,
>> > I am trying to output a String that I can pass to another method I
[quoted text clipped - 43 lines]
> 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_20N
ewsgroups


   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
sks - 07 Aug 2006 21:45 GMT
> 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")
Moiristo - 08 Aug 2006 11:03 GMT
>> Hello,
>>
[quoted text clipped - 5 lines]
> 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.
Mark Space - 08 Aug 2006 00:42 GMT
> Hello,
> I am trying to output a String that I can pass to another method I
[quoted text clipped - 14 lines]
>                                         //           String exceptionString
>// = new String("UPC should be all Numeric");
                                throw new IllegalArgumentException(
"UPC should be all numeric.");
>                 //return exceptionString;     //Type mismatch
>                 //return false;
[quoted text clipped - 5 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.