Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to switch between the created 'Account' objects? I have a class before this one that gets all the required information, and the code that you see here is in the main method. * There are variables in the main method before this while-loop, but I'm not having trouble with that part of my code.


```    
while(answer == 1)
            
    
            {
                        System.out.print("Select from your created accounts: ");//the user can select from their creat accounts using by pressing the account number
        //the account number variable is in 'Account' class and it increments by one each time the account object is called
                        for(int i = 0; i < accounts; i++)//prints the name of the created user accounts and selection number
                        {
                            System.out.print(users[i].name + "(" + users[i].selection + ")" + " ");
                        }
                        pick = input.nextInt();
                        for(int i = 0; i < accounts; i++)
                        {
                            while(pick == users[i].selection)//displays these options if the pick variable == the selection variable 
                            {
                                System.out.println("A. Display account information");
                                System.out.println("B. Check account balance");
                                System.out.println("C. Make a deposit");
                                System.out.println("D. Make a withdrawal");
                                System.out.println("E. Change adress");
                                System.out.println("F. Exit");
                                
                                answer = input.next().charAt(0);
                                answer = Character.toUpperCase(answer);
                                switch(answer)//lets user select an option
                                {
                                    case 'A':
                                    users[i].displayInfo();
                                    break;
                                    
                                    case 'B':
                                    users[i].checkBalance();
                                    break;
                                    
                                    case 'C':
                                    users[i].deposit();
                                    break;
                                    
                                    case 'D':
                                    users[i].withdraw();
                                    break;
                                    
                                    case 'E':
                                    users[i].changeAdress();
                                    break;
                                    
                                    case 'F':
                                    System.out.println();
                                    break;
                                    
                                    default:
                                    System.out.println("Please enter one of the options listed.");
                                    break;
                                }

                                //when the user presses 'F' or 'f', I want the compiler to go back to print 'select from your accounts' and then let the user select an account to manipulate its information
                                //it works the first time through, but after that, it's stuck in a loop where it just displays all the options for the account
                            }
                        }
```


What I have tried:

I tried putting a break statement after the while-loop so that when the user presses 'f' or 'F', it displays the select account option, so that the the user can pick between their created objects, but it prints 'select from your created accounts' - which is what I want it to do, but when the user presses corresponding selection variable for the created 'Account' object, but it goes in a loop of displaying 'select account'.
*If requested, I can post all of the code so that you can run it on a compiler, and be able to make changes as you see fit.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900