1

I want to make an array of integer values that represent counters so I initialized it like this in my main method:

int [] counters = new int [7];
        counters [countListAll] = 0;
        counters [countEmployeeReport] = 0;
        counters [countDivisionReport] = ;
        counters [countSalaryReport] = 0;
        counters [countRetirementReport] = 0;
        counters [countMain] = 0;
        counters [countOthers] = 0; 

And now I have to pass this array to my menu() method so that I can increment each counter each time any of the options is chosen.

public static void menu(int [] counters)

    {

        System.out.println("You have accessed Menu()");
        System.out.println("Enter 'L' for list of the employee data available. \nEnter 'E' to dislpay information on a particular employee. \nEnter 'D' to display division information. \nEnter 'S' to display salary information. \nEnter 'R' to display retirement information. \nEnter 'Q' to quit Menu and return to Main.");      
        Scanner scan = new Scanner(System.in);
        String first = scan.next();
        char first1 = first.charAt(0);

        if (first1 == 'L'|| first1 =='l'||first1 =='E'||first1 =='e'||first1 =='D'||first1 =='d'||first1 =='S'||first1 =='s'||first1 =='R'||first1 =='r'||first1 =='Q'||first1 =='q')
        {
switch (first1)
                {
            case 'L':
            case 'l':
            listAll();
            counters [countListAll] ++;
            break;

            case 'E':
            case 'e':
            employeeReport();
            counters [countEmployeeReport] ++;
            break;

            case 'D':
            case 'd':
            divisionReport();
            counter [countDivisionReport] ++;
            break;

            case 'S':
            case 's':
            salaryReport();
            counters [countSalaryReport] ++;
            break;

            case 'R':
            case 'r':
            retirementReport();
            counters [countRetirementReport] ++;
            break;

            case 'Q':
            case 'q':
            counters [countMain] ++;
            break;

            else 
            {
                menu();
                countOthers++;
            }
    }

Am I initializing the array correctly and passing it into menu() method correctly? And am I allowed to increment the objects like that?

EDIT: I changed the code, and this is the new code

Main method:

 int [] counters = new int [7];
            counters [L] = 0;//listAll
            counters [E] = 0;//employeeReport
            counters [D] = 0;//divisionReport
            counters [S] = 0;//salaryReport
            counters [R] = 0;//retirementReport
            counters [Q] = 0;//quit
        counters [O] = 0;//others

Menu Method:

public static void menu(int [] counters)

    {
        System.out.println("You have accessed Menu()");
        System.out.println("Enter 'L' for list of the employee data available. \nEnter 'E' to dislpay information on a particular employee. \nEnter 'D' to display division information. \nEnter 'S' to display salary information. \nEnter 'R' to display retirement information. \nEnter 'Q' to quit Menu and return to Main.");      
        Scanner scan = new Scanner(System.in);
        String first = scan.next();
        char first1 = first.charAt(0);

        if (first1 == 'L'|| first1 =='l'||first1 =='E'||first1 =='e'||first1 =='D'||first1 =='d'||first1 =='S'||first1 =='s'||first1 =='R'||first1 =='r'||first1 =='Q'||first1 =='q')
        {
            switch (first1)
                {
            case 'L':
            case 'l':
            listAll();
            counters [L] ++;
            break;

            case 'E':
            case 'e':
            employeeReport();
            counters[E]++;
            break;

            case 'D':
            case 'd':
            divisionReport();
            counters [D]++;
            break;

            case 'S':
            case 's':
            salaryReport();
            counters [S]++;
            break;

            case 'R':
            case 'r':
            retirementReport();
            counters [R]++;
            break;

            case 'Q':
            case 'q':
            counters [Q]++;
            break;

                }
        }

        else 
            {
                menu();
                counters [O]++;
            }
    }

And FinalStats Method:

public static void finalStats(int [] counters)
    {
        System.out.println("Number of times listAll() was accessed from menu() is: " + counters[L]);
        System.out.println("Number of times employeeReport() was accessed from menu() is: " + counters[E]);
        System.out.println("Number of times divisionReport() was accessed from menu() is: " + counters[D]);
        System.out.println("Number of times salaryReport() was accessed from menu() is: " + counters[S]);
        System.out.println("Number of times retirementReport() was accessed from menu() is: " + counters[R]);
        System.out.println("Number of times 'Quit' was chosen from menu() is: " + counters[Q]);
        System.out.println("Number of times any other key was pressed in menu() is: " + counters[O]);
    }
2
  • 1
    You're doing it correctly, except for the countOthers, if that's also to be maintained in array the you should be using counters[countOthers]++ Commented Dec 7, 2015 at 4:55
  • @11thdimension Thank You! I fixed it. Commented Dec 7, 2015 at 4:58

1 Answer 1

2

Yes, in general. There appears to be a bug with your countOthers logic. although nesting a switch with-in an if seems an odd choice, it would be much more readable with a simple if else-if chain (and you could use Character.toLowerCase(char) to handle mixed case. Something like,

if-else

public static void menu(int[] counters) {
    System.out.println("You have accessed Menu()");
    System.out.println("Enter 'L' for list of the employee data available.");
    System.out.println("Enter 'E' to dislpay information on a particular employee.");
    System.out.println("Enter 'D' to display division information.");
    System.out.println("Enter 'S' to display salary information.");
    System.out.println("Enter 'R' to display retirement information.");
    System.out.println("Enter 'Q' to quit Menu and return to Main.");
    Scanner scan = new Scanner(System.in);
    String first = scan.next();
    char first1 = Character.toLowerCase(first.charAt(0));
    if (first1 == 'l') {
        listAll();
        counters[countListAll]++;
    } else if (first1 == 'e') {
        employeeReport();
        counters[countEmployeeReport]++;
    } else if (first1 == 'd') {
        divisionReport();
        counter[countDivisionReport]++;
    } else if (first1 == 's') {
        salaryReport();
        counters[countSalaryReport]++;
    } else if (first1 == 'r') {
        retirementReport();
        counters[countRetirementReport]++;
    } else if (first1 == 'q') {
        counters[countMain]++;
    } else {
        menu();
        counters[countOthers]++; // <-- instead of countOthers++
    }
}

switch-case

It is also possible to express the above if-else chain with switch-case and something like,

switch (Character.toLowerCase(first.charAt(0))) {
case 'l':
    listAll();
    counters[countListAll]++;
    break;
case 'e':
    employeeReport();
    counters[countEmployeeReport]++;
    break;
case 'd':
    divisionReport();
    counter[countDivisionReport]++;
    break;
case 's':
    salaryReport();
    counters[countSalaryReport]++;
    break;
case 'r':
    retirementReport();
    counters[countRetirementReport]++;
case 'q':
    counters[countMain]++;
    break;
default:
    menu();
    countOthers++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.