4

Here is my code. I am not sure how to exit the program using a return value. Any ideas? THis is the last step from my assignment. Important areas are marked with /////// I hear return works but when I change void in main to int, the program says main must be void.

import java.util.Scanner;


public class CommissionCalculator {

    public static void main(String args[]) {
        // Initialize a Scanner to read input from the command line
        Scanner ItemSelect = new Scanner(System.in);
        double comp = 200.00;
        double item1 = 239.99;
        double item2 = 129.75;
        double item3 = 99.95;
        double item4 = 350.89;
        double comm = 0.09;
        int choice;


        /* Note that we'll be doing this at least once and most likely multiple times...
         * Prompt the user with a menu of the four items and their values (this information is included in the problem statement)
         */
        System.out.println("Item\tValue");
        System.out.println("1\t$239.99");
        System.out.println("2\t$129.75");
        System.out.println("3\t$99.95");
        System.out.println("4\t$350.89"); 
        /* Display the user's current compensation */
        System.out.printf("Current compensation: $%.2f", comp);


        /* 
         * Prompt and take input from the user (you may assume that they will only enter int values)
         * They'll enter an item number (1 - 4) to record its sale or 0 to exit
         * 
         * NOTE: THE U0SER DOES NOT ENTER PRICES DIRECTLY... THEY ENTER ITEM NUMBERS TO INDICATE WHAT WAS SOLD
         * NOTE: THE USER MAY ENTER THE SAME ITEM NUMBRER MULTIPLE TIMES
         * 
         * If the user provides invalid input (a value other than 0 - 4) display "ERROR: Invalid input!" and prompt them again
         */
        do
        {

            System.out.print("\nPlease select an item from the " +
                               "list above (or enter 0 to exit): ");
            choice = ItemSelect.nextInt();

            {   
                if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0)
                {
                    do
                      {
                        System.out.print("ERROR: Invalid Input!\n");
                        System.out.println("Item\tValue");
                        System.out.println("1\t$239.99");
                        System.out.println("2\t$129.75");
                        System.out.println("3\t$99.95");
                        System.out.println("4\t$350.89");
                        System.out.printf("Current compensation: $%.2f", comp);
                        System.out.print("\nPlease select an item from the " +
                                   "list above (or enter 0 to exit): ");
                        choice = ItemSelect.nextInt();
                        if (choice == 1)
                        {
                            comp += (comm * item1);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 2)
                        {
                            comp += (comm * item2);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 3)
                        {
                            comp += (comm * item3);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 4)
                        {
                            comp += (comm * item4);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 0)
                        {
                            System.out.printf("Total Earnings: $%.2f", comp);
                            System.exit(0); ///////
                        }

                      }while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0); 

                }
                else
                {
                    if (choice == 1)
                    {
                        comp += (comm * item1);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 2)
                    {
                        comp += (comm * item2);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 3)
                    {
                        comp += (comm * item3);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 4)
                    {
                        comp += (comm * item4);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 0)
                    {
                        System.out.printf("Total Earnings: $%.2f", comp);
                        System.exit(0);
                    }
                }

            }
        }while (choice != 0);

        /* After the user enters 0, display the salesperson's earnings in the format "Total earnings: $NNN.NN" and exit
         * For example, if the salesperson sold two item 3s this week the final output would be "Total earnings: $217.99"
         */
        if (choice == 0)
        {
            System.out.printf("Total Earnings: $%.2f", comp);
            System.exit(0);   ///////
        }
        ItemSelect.close();
        System.exit(0);    ///////
    }
}
3
  • 3
    The only way to return a value back to the calling program is to use System.exit. If, however, you want to access this method from another class, for example, then you should move the contents to another method capable of returning the value you want and then you could this from your main method if you needed to or from another class. In this way, you could use return Commented Nov 6, 2012 at 7:54
  • This is it. Why didn't you answer, I fixed it. Commented Nov 6, 2012 at 8:05
  • I didn't answer as the question was a little vague (IMHO), but if you liked the comment, give it a little up vote ;) Commented Nov 6, 2012 at 8:06

2 Answers 2

13

As long as you don't need to return custom exit code (other than 0, as returned by System.exit(0)) and don't start new threads, you can terminate your program by doing

return; 

in your main() method. Note that there is no value returned, so that you don't need to change your main() method from void to int.

Sign up to request clarification or add additional context in comments.

Comments

0

If you wrote your program in a logical manner, where your code flows properly, you don't need exit() or return, unless the method is supposed to return something at the end, in which case, you would just have one return statement at the end of your method, e.g., use a switch with a case for 0 instead of stacked if constructs which are inefficient anyway. The cases do whatever calculations and output and case 0 does what you have after the loop ends except for the exit() calls, which are not needed. Once the loop ends, the method ends.

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.