0

I have been stuck on this one for days, but I have broken it down here. What I need to do is to create an array of accounts with about 9 variables each (AccountID, WithdrawlDates, etc.) that the user can input in a command prompt. From the createAccount() method I can send an instance of user and a accountNum, but the user is not recognized on the receiving setAccount method.

Here's the code:

class User{

   private int accountID;

   User( int id )
   {
       accountID = id;
   }

   static void setAccountID(User user[], int accountNum)
   {       
       user.accountID = accountNum; //accountID is not recognized here
   }
   static void getAccountID(User user){System.out.println(user.accountID);}
   }

class TestUser
{
   public static void main(String[] args)
   {       
      createAccount();
   }

   static void createAccount(){
       User[] user = new User[2];
       user[0] = new User(25);
       User.setAccountID(user, 2001); 
   }
}

I am open to changing the flow of this, but I don't know where to start.

Thanks!

2 Answers 2

1

To access the elements of an array instead of doing something with the array itself you use square brackets like so:

user[userIndex]

from there you can either change the element like this

user[userIndex] = new User(id);

or access/modify something about the element itself like this

user[userIndex].accountID = whatever;

Additionally, your use of static in the setAccountID is confusing things. A static method cannot know anything about accountID because accountID is a part of a uniquely created object where the static method belongs to the class, and not any particular object. If it must be static for some reason, you will need to change the method to look something like this

static void setAccountID(User user[], int userIndex, int accountNum)
{       
   user[userIndex].accountID = accountNum;
}

but the following would be much better, since you know the user inside the array anyway:

void setAccountID(int accountNum)
{       
   this.accountID = accountNum;
}

called like this:

user[userIndex].setAccountID(accountNum);
Sign up to request clarification or add additional context in comments.

Comments

1

There's no reason to pass an array of User objects. Try this instead:

class User{

   private int accountID;

   User( int id )
   {
       accountID = id;
   }

   static void setAccountID(User user, int accountNum)
   {       
       user.accountID = accountNum; //accountID is not recognized here
   }
   static void getAccountID(User user){System.out.println(user.accountID);}
   }

class TestUser
{
   public static void main(String[] args)
   {       
      createAccount();
   }

   static void createAccount(){
       User user = new User(25);
       User.setAccountID(user, 2001); 
   }
}

EDIT: If you need to maintain an array of users as @Luiggi Mendoza suggests in his comment, just pass a single array element to setAccountID():

static void createAccount(){
   User[] user = new User[2];
   user[0] = new User(25);
   User.setAccountID(user[0], 2001); // set id for first User
}

5 Comments

He doesn't need to send the User array, but he needs to maintain the array for the whole program.
@LuiggiMendoza - Maybe so. I added a suggestion for how to deal with that.
I do need to keep the array of accounts somewhere.
If the 'User' array isn't sent to the 'setAccountID()' method, then where would the array be maintained for the whole program?
@user1227370 - Since most of your code is static, I would suggest a static variable in class TestUser. You could then just pass to setAccountID (or any other method) an index of the user that is to be affected by the call and it could index into the array. Generally, though, this is somewhat poor design. You would be better off making setAccountID an instance method of class User and then you could invoke it with things like users[0].setAccountID(newID);.

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.