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 / January 2005

Tip: Looking for answers? Try searching our database.

Switch statements?

Thread view: 
JS - 19 Jan 2005 22:27 GMT
In my method VoteTest I would like to have a case that throws an exception
with some information to the user if a is a number different from 1 or 2 ( a
= = 1 || a = =2). Is that possible?

public class VoteTest {

   private int knud;
   private int jesper;

   public void test(int a){
       switch(a)
       {
       case 1:
           knud = knud + 1;
           break;
       case 2:
           jesper = jesper + 1;
           break;

       }
   }
Eric Sosman - 19 Jan 2005 22:59 GMT
> In my method VoteTest I would like to have a case that throws an exception
> with some information to the user if a is a number different from 1 or 2 ( a
[quoted text clipped - 6 lines]
>
>     public void test(int a)
         throws StupidVoterException
> {
>         switch(a)
[quoted text clipped - 5 lines]
>             jesper = jesper + 1;
>             break;
         default:
             throw new StupidVoterException(
                 "Why waste your vote on " + a + "?");

>         }
>     }

   This assumes that the StupidVoterException class (or whatever
other Exception you decide to use) has a constructor that takes
a String argument.  Most (perhaps all) Exceptions have such a
constructor, and any that don't ought to (see the Javadoc for
Throwable).

Signature

Eric.Sosman@sun.com

Alex Kizub - 19 Jan 2005 23:58 GMT
> In my method VoteTest I would like to have a case that throws an exception
> with some information to the user if a is a number different from 1 or 2 ( a
> = = 1 || a = =2). Is that possible?

public class VoteTest {

   private int knud;
   private int jesper;

   public static void main (String []a) {
       VoteTest t=new VoteTest();
       t.test(1);
       t.test(2);
       t.test(3);
   }

   public void test(int a){
       switch(a)
       {
       case 1:
           knud = knud + 1;
           break;
       case 2:
           jesper = jesper + 1;
           break;
       default: throw new RuntimeException("Error: "+a+"is not 1 or 2!");

       }
   }
}

Alex Kizub.


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.