1

I wrote this code using NetBeans 7.3. It is an elustration for a simple ATM program. My problem is I cannot view the menu options more than twice. In the second time it is repeated I cannot inter the switch. What can I do to fix this problem??

This is my code:

public static void main() {

    System.out.println("            ****************************************************" );
    System.out.println("            * Can you please choose which one of the following *" );
    System.out.println("            *   services you want the program to perform by    *" );
    System.out.println("            *    typing down the number of the option below:   *" );
    System.out.println("            *                                                  *" );
    System.out.println("            *       1. Test credit card number.                *" );
    System.out.println("            *       2. Exit.                                   *" );
    System.out.println("            ****************************************************" );

    int choice;
    System.out.print("your choice is: ");
    choice = console.nextInt();
    //while (choice == 1 || choice != 2)
    if (choice == 2) {
        System.out.println("                 *** Please visit us again. ***");
        System.exit(0);        
    } 
}

public static void main(String[] args) {

    int choice; 
    System.out.println("            *****************************************************" );
    System.out.println("            *   Welcome to the credit card number test program  *" );
    System.out.println("            *                                                   *" );
    System.out.println("            *    First we would like to thank you for choosing  *" );
    System.out.println("            *  our program and we hope you will find it useful  *" );
    System.out.println("            *                                                   *" );
    System.out.println("            *  We guarantee you that you will receive the best  *" );
    System.out.println("            *               services in the world.              *" );
    System.out.println("            *****************************************************" );


    System.out.print("your choice is: ");
    choice = console.nextInt();

    switch (choice) {   
        case 1:
        int[][] credit_number = new int [3][16];
        int row;
        int col;
        int sum;
        String statue;

        System.out.println("Please enter 16 number for a credit card: " ); 

        row = 0;
        {    
            for (col = 0; col < credit_number[row].length; col++)
            credit_number[row][col] = console.nextInt();
        }     

      while (choice == 1 || choice != 2)
          main();
      System.out.println();
      break;      

  case 2:
      System.out.println("                 *** Please visit us again. ***");
      System.exit(0);

      default: {
          System.out.println("Warning: Please make sure to choose an available option from the menu.");
          main();
      }
   }
}}
3
  • 4
    Am I the only one that finds this use of two overloaded main functions ugly? Commented Oct 9, 2013 at 5:06
  • I was about to comment the same as @Floris. I dont know enough java rules to determine how or when the second main function even gets called from the first? This surely has to be the source of your issues. Instead of recursively callibg main, put the whole construct in a while loop -- looks like you were headed that way once based on commented lines. And stick with one main, and name other methods something else. Call them explicitly as needed. Commented Oct 9, 2013 at 5:20
  • @Floris, yes you are, I always call all my functions main. And when the signatures are the same I call them main1, main2 etc. It works for me. Commented Oct 9, 2013 at 5:28

1 Answer 1

1

Your code is confused. You've got 2 routines named main.

The main with the following signature is your proper main function, which gets called when starting the application:

public static void main(String[] args) {

So this gets called first.
Inside this function you call the other main() let's call it main2 to avoid confusion.

In main2 you call exit which terminates the program.

So it's exactly right that your program only runs twice.

You can fix the problem by making your program a sane one.

  1. You should never repeat yourself.
  2. Use meaningful names.
  3. Have functions return values.
  4. Remember that variables local to a function are invisible outside that function (google +java +scope +variable)

The structure should go like this:

public static void main(String[] args) {

  boolean areWeDoneYet = false;
  string ccNumber;

  while !(areWeDoneYet) {
    displayMenu();
    int choice = getUserInput();
    switch (choice) {
      case 1: 
        ccNumber = getCreditCardNumber();
        processCreditCardNumber(ccNumber);  
        break;
      case 2:  
        areWeDoneYet = true;
        break; 
      default: 
        displayErrorMessage();
        //waitForUserToConfessHisSins();
        //fineUser();
        //questionMark();
        //dots();
        //profit();
    } //switch 
  } //while
  exit(0);
}

Then you create functions for displayMenu(), getUserInput(), getCreditCardNumber(), displayErrorMessage().
Note that all the *get*Functions must return whatever it is that they're supposed to get.

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

1 Comment

This code structure looks a lot more readable. I am sure it will help.

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.