Switch Statement..return problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Switch Statement..return problem

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Switch Statement..return problem

    Hi,

    I am having a problem with a switch statement I have coded and have not found sample code to help me figure out exactly what I am doing wrong. The code is supposed to take a 2 dimensional array (String Array[4][7]) index i.e. rowNum and colNum and return a number between 1 and 28. The method requires an int return. When I pass the cursor over the end brace it says it is missing return statement. Here is the code... if you can see what I have done wrong please help!

    public int findDayNum(int rowNum, int colNum){
    switch (rowNum){
    case 0:
    if (colNum == 0) {return 1;}
    else if (colNum == 1) {return 2;}
    else if (colNum==2) {return 3;}
    else if(colNum==3) {return 4;}
    else if (colNum == 4) {return 5;}
    else if (colNum == 5) {return 6;}
    else if (colNum == 6) {return 7;}
    break;
    case 1:
    if (colNum == 0) {return 8;}
    else if (colNum == 1) {return 9;}
    else if (colNum==2) {return 10;}
    else if(colNum==3) {return 11;}
    else if (colNum == 4) {return 12;}
    else if (colNum == 5) {return 13;}
    else if (colNum == 6) {return 14;}
    break;
    case 2:
    if (colNum == 0) {return 15;}
    else if (colNum == 1) {return 16;}
    else if (colNum==2) {return 17;}
    else if(colNum==3) {return 18;}
    else if (colNum == 4) {return 19;}
    else if (colNum == 5) {return 20;}
    else if (colNum == 6) {return 21;}
    break;
    case 3:
    if (colNum == 0) {return 22;}
    else if (colNum == 1) {return 23;}
    else if (colNum==2) {return 24;}
    else if(colNum==3) {return 25;}
    else if (colNum == 4) {return 26;}
    else if (colNum == 5) {return 27;}
    else if (colNum == 6) {return 28;}
    break;
    default: return -1;

    }//end switch
    }//end findDayNum

  2. #2
    Join Date
    Oct 2006
    Posts
    2

    Think I have figured it out...

    Hi,

    Thanks for looking at this however, I see that the return statement, which must be the last statement ... cannot be the last statement in code like this. I have made the method a void proceedure and declared a variable to hold the answer as my solution to this problem.

    Many thanks..

  3. #3
    Join Date
    Sep 2006
    Posts
    68
    ...
    }//end switch
    return -1;
    // }//end findDayNum

  4. #4
    Join Date
    Apr 2011
    Posts
    1
    Yes, I do realize it's been 5 years...BUT! I just HAD to register here and write this -

    return rowNum*7 + colNum + 1;

    instead of all that crap above

Similar Threads

  1. Problem with SQL 'LIKE' Statement
    By silencer01 in forum .NET
    Replies: 1
    Last Post: 08-11-2005, 10:00 AM
  2. String conditional statement problem
    By Drakenza in forum C++
    Replies: 3
    Last Post: 08-01-2005, 09:05 PM
  3. PROBLEM using ALIAS with a WHERE STATEMENT
    By Katovski in forum Database
    Replies: 3
    Last Post: 04-02-2003, 02:39 PM
  4. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 04:30 PM
  5. ASP insert statement problem
    By Ken Mui in forum ASP.NET
    Replies: 1
    Last Post: 10-06-2000, 06:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center