kadinthedev kadinthedev - 1 year ago 77
Java Question

Need existing user to be able to view data from application using arrays

I am making a simple banking application for my computer science class but am not sure how I can store user account details in an array and access it when they sign in. Here is my code.

double balance = 0.00;
String name = null;
String menuOptions;
Account[] nameUser = new Account[1000];

System.out.println("Welcome! To create an account please press 1, if existing user press 2.");
Scanner keyboardAccount = new Scanner(System.in);
double a = keyboardAccount.nextDouble();

//If/else prompts user to create account
if (a == 1.0) {
System.out.println("Please enter your full name.");
Scanner keyboardName = new Scanner(System.in);
name = keyboardName.next();
//add this to array of Accounts
nameUser[1] = new Account();
} else if (a == 2.0) {
System.out.println("Please enter your full name.");
Scanner keyboardName2 = new Scanner(System.in);
name = keyboardName2.next();
System.out.println("Welcome user " + name + "!");
else {
System.out.println("Please make an account.");

Answer Source

You will need something called dynamic arrays so you don't need to know how exact long your array should be

The basic one is called ArrayList<>

So in your example code will look like this. Initialize Array

ArrayList<String> users = new ArrayList<>();

Add users


Retrieve user

for(String userFromArray : users){ // Loop over this Array
            System.out.println("You are signed in!");

            // no need to loop more so break from the loop


Hope that I did not misunderstood your question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download