0

I'm a bit lost; I feel like I've done some things similar to this before, but this doesn't work. Is there some adjustment I'm forgetting? For some reason It's saying It can't find the three variables below, Do I need to move them elsewhere/declare in method/somehow disjoint the method from the variable call? All help greatly appreciated.

Scanner input = new Scanner(System.in);
        int atmId, selection;
        double atmVal;
    selectId();

        switch(selection){
            case 1:  AccArray[atmId].getBalance();
                    break;
            case 2: System.out.print("How much would you like to withdraw? ");
                    atmVal=input.nextInt();
                    AccArray[atmId].withdraw(atmVal); 
                    break;
            case 3:  System.out.print("How much would you like to deposit? ");
                    atmVal=input.nextInt();
                    AccArray[atmId].deposit(atmVal);
                    break; 
            case 4:  selectId();
                    break;
        }


    } // End of main method

// Method for id selection
    public static void selectId(){
// Allows for and requests user input       
        System.out.print("Enter an id: ");
        atmId = input.nextInt();

// Checks for valid input       
        while (atmId>9 || atmId<0){
            System.out.println("Invalid id.");
            System.out.print("Enter an id (0-9): ");
            atmId = input.nextInt();
        }
        displayMenu();
    }

// Method to display menu
    public static void displayMenu(){
        System.out.println("Main menu");
        System.out.println("1: check balance");
        System.out.println("2: withdraw");
        System.out.println("3:deposit");
        System.out.println("4:exit");
        System.out.print("Enter a choice ");
        selection = input.nextInt();
        // Checks for valid input
         while (selection>4 || selection<1){
            System.out.println("Invalid choice.");
            System.out.print("Enter a choice (1-4): ");
            selection = input.nextInt();
        }
    }
} // End of Test class
1
  • If these variables are declared in main, then they're not visible in any other method. Apr 14, 2014 at 0:31

1 Answer 1

0

Note that you have some scoping errors -- variables declared inside of a method are visible only within that method and are not visible in other methods. To solve this, you could declare the variables within the class.

For example, using only static fields and methods:

public class Foo {
  private static int bar; // I am visible throughout the class

  public static void someMethod() {
    int baz = 8;  // I've declared a *local* variable here
  }

  public static void anotherMethod() {
    bar = 3;  // I can use bar here since it's visible throughout

    baz = 7; // this won't compile since baz is visible only within someMethod()
  }
}

Having said that, though shouldn't your selectId() method return an int? Shouldn't you then assign the returned int to your selection variable? If you did this, then the selection variable would not have to be declared in the class.

Note that I would have displayMenu() do just that -- display a menu, and would not have it get user input. That would be for the selectId() method to do.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.