Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2010
    Location
    Belfast
    Posts
    1
    Rep Power
    0

    Default Switch Statement/Parameter Passing

    Hi guys,

    I am trying to use parameter passing to call a method within a switch statement. I can call the method however non of the arrays are displayed, its only displays the string at the end.

    This is the code i am having the trouble with.
    menuOption = 0;


    do
    {

    optionValid =true;
    //call display menu method
    displayMenu();
    //prompt for user input of menu option
    System.out.println(".............................. .................");
    System.out.println("Please type in a menu option");
    System.out.println(".............................. .................");
    menuOption=InOut.readInt();

    //while statement to validate if menu option is within range
    while ((menuOption < 0) || (menuOption > 5))

    System.out.println(".............................. .................");
    System.out.println("Invalid value. This should in between 1 and 5");
    System.out.println(".............................. .................");
    //call display menu method
    displayMenu();
    //Prompt user for input of menu option
    menuOption=InOut.readInt();
    //end of while loop

    //start of switch statement
    switch (menuOption)
    {
    case 1:displayEmployees(spRefNo,spName, spMonthlySales, spGrossPay, spDeductions, spNetPay,0,20000,"All Employee Details");
    break;

    case 2: displayEmployees(spRefNo, spName, spMonthlySales, spGrossPay,spDeductions, spNetPay, 10000, 20000,"Employees whose monthly sales are 10000 or above");
    break;

    case 3: averageSales = averageResult(spMonthlySales, NUMBEROFEMPLOYEES);
    displayEmployees(spRefNo, spName, spMonthlySales, spGrossPay, spDeductions, spNetPay, 0, averageSales,"Employees whose monthly sales are below the average");
    break;

    case 4: for (index = 0; index <= NUMBEROFEMPLOYEES -1; index++)
    {
    if (spMonthlySales[index]==highestSales)
    {
    spNetPay[index]=spNetPay[index] + 500;
    System.out.println(spRefNo[index] + "\t " + spName[index] + "\t " + spMonthlySales[index] + "\t\t " + spGrossPay[index] + "\t " + spDeductions + "\t " + spNetPay[index]);
    }//end of if statement to find employee with highest sales

    }
    break;//End of for loop

    case 5:optionValid=false;

    }//End of menu switch statement

    }while(menuOption<=4);//end of while statement

    }//End of main method

    This is the method i am trying to call.

    static void displayEmployees (int spRefNo[] ,String spName[],double spMonthlySales[],double spGrossPay[],double spDeductions[],double spNetPay[],int lowValue,double highValue,String message)
    {
    int index;
    int NUMBEROFEMPLOYEES = 0;
    System.out.println("" + message + "");

    for (index = 0; index <= NUMBEROFEMPLOYEES -1; index =index + 1)
    {
    if ((spMonthlySales[index] >= lowValue) && ( spMonthlySales[index] < highValue))
    {
    System.out.println(spRefNo[index] + "\t " + spName[index] + "\t " +spMonthlySales[index] + "\t " + spGrossPay[index] + spDeductions + "\t " + spNetPay[index]);
    }//End of if construct

    }//End for for loop


    }//End of display employees menthod

    If you could help it would be fantastics. thanks

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

    Default

    Put System.out.println statements to see where your code is going and what are the values of certain variables.
    Also given that NUMBEROFEMPLOYEES start at 0 what do you think is happening to the for loop?
    More importantly, start the exercise again using object oriented principles instead.
    Create an Employee class that has appropriate properties to model the data you are dealing with. Those arrays are older than oldskool.

Similar Threads

  1. passing parameter to a thread
    By adammyth in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-02-2010, 08:58 PM
  2. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 06:46 AM
  3. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 06:27 AM
  4. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 02:44 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 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
  •