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 / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

What is wrong with this function??

Thread view: 
bunallo - 26 Feb 2005 18:58 GMT
This function will not compile:

   public int getInt(int p){

       switch(p){
       case 1:
           return 2;
       case 2:
           return 3;
       case 3:
           return 3;
       default:
           System.out.println("No such input allowed");
       }
   }

But it returns an int!
Tilman Bohn - 26 Feb 2005 19:20 GMT
[...]
>         default:
>             System.out.println("No such input allowed");
[...]
> But it returns an int!

Can you see the disparity between reality and your claim? ;-)

Signature

Cheers, Tilman

`Boy, life takes a long time to live...'      -- Steven Wright

bunallo - 27 Feb 2005 00:41 GMT
> [...]
> >         default:
[quoted text clipped - 3 lines]
>
> Can you see the disparity between reality and your claim? ;-)

I can see the problem but it seems odd just to return an arbitrary integer
in the default case....
Patricia Shanahan - 27 Feb 2005 01:36 GMT
>>[...]
>>
[quoted text clipped - 9 lines]
> I can see the problem but it seems odd just to return an arbitrary integer
> in the default case....

So what should it do for that case? After you output the
message, what is your program supposed to do next? Maybe you
should consider throwing an exception, so that the caller
also knows what went wrong? Or calling System.exit, if it
is definitely fatal?

Patricia
Tilman Bohn - 27 Feb 2005 01:51 GMT
[...]
>> >         default:
>> >             System.out.println("No such input allowed");
[...]
> I can see the problem but it seems odd just to return an arbitrary integer
> in the default case....

 There are two possibilites: Either the default case is to be regarded
as something more or less `normal'. Then you should return an appropri-
ate value. Or the default case is something exceptional that you cannot
handle. Then you should throw an appropriate Exception. There is no
third way for a method to exit, and certainly System.out.println() is
not such a third way.

Signature

Cheers, Tilman

`Boy, life takes a long time to live...'      -- Steven Wright

klynn47@comcast.net - 26 Feb 2005 20:30 GMT
You have to make sure that you have a return for all cases. There is no
return for the default case.
Tor Iver Wilhelmsen - 27 Feb 2005 09:37 GMT
> You have to make sure that you have a return

... or an exception throw...

> for all cases. There is no return for the default case.
Hal Rosser - 26 Feb 2005 21:33 GMT
> This function will not compile:
>
[quoted text clipped - 13 lines]
>
> But it returns an int!

Does it compile?
default needs to return an int.
you need to validate the (int p) arg before it gets to the method


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



©2008 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.