Results 1 to 3 of 3
  1. #1
    monia is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default Case with return statement within abstract

    Hello,
    I shall be grateful about the clarification of the mistakes I am making in the case statement with Random Math and return . The following case statement where the compiler complains about ' unreachable statements' as error is :
    "public class Flowers
    {
    static String[] flowerProviders = {"Joe's Nursery", "Plants R Us", "Fred's Plant World", "Golden

    Nurseries", "Plants of the World"};
    static String[] flowerPrices = {"$2.50", "$3.75", "$4.50", "$5.25", "$5.50"};
    static String[] flowerColors = {"Scarlet", "Crimson", "Purple", "Orange", "Yellow"};

    static final int TOTAL_PROVIDERS = flowerProviders.length;

    public static Flower findFlowers()
    { //Create switch statement
    int whichFlower = (int)(Math.random()*5);

    switch (whichFlower)

    {

    case 0:
    return new Begonia(flowerProviders[(int)(Math.random()*TOTAL_PROVIDERS)],
    flowerPrices[0], flowerColors[0]);
    break;
    case 1:
    return new Coleus(flowerProviders[(int)(Math.random()*TOTAL_PROVIDERS)],
    flowerPrices[1], flowerColors[1]);
    break;
    case 2:
    return new Geranium(flowerProviders[(int)(Math.random()*TOTAL_PROVIDERS)],
    flowerPrices[2], flowerColors[2]);
    break;

    case 3:
    return new Marigold(flowerProviders[(int)(Math.random()*TOTAL_PROVIDERS)],
    flowerPrices[3], flowerColors[3]);
    break;
    case 4:
    return new Sunflower(flowerProviders[(int)(Math.random()*TOTAL_PROVIDERS)],
    flowerPrices[4], flowerColors[4]);
    break;
    default:
    return null;
    }

    }
    public static void main(String[] args)
    {
    int userInputNum = Integer.parseInt(args[0]);
    Flower[] flowerArray = new Flower[userInputNum];
    int i = 0;

    //Create first for statement
    for ( i = 0; i< flowerArray.length; i++)
    {
    flowerArray[i]=findFlowers();
    }

    ...... etc
    "
    I look forward for some guidelines.
    Kind regards,
    monia

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The compiler is just being smart. A return statement asks the JVM to stop executing the current method immediately. If you have code after the return statement the compiler realizes that that code will never be reached because the return has already happened so it tells you that and waits until you fix it.

  3. #3
    monia is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default good point about case return

    Thank you,
    it is solved now
    kind regards,
    Monia:)

Similar Threads

  1. case statement
    By skiing in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 01:13 PM
  2. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 07:29 PM
  3. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 05:55 PM
  4. [SOLVED] return statement
    By Nakira in forum New To Java
    Replies: 8
    Last Post: 11-13-2008, 12:00 AM
  5. Can I set a range in case statement?
    By christina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •