clientData[] firstClient = new clientData[0];
.... defines an array of size 0, not big enough to be useful!
You may prefer something like
clientData[] manyClients = new clientData[100]; // array to hold up to 100 clients
Then you can simply do stuff like
manyClients[0] = new Client(...) // create new client and store in array
System.out.println(manyClients[0].getClientForename()); //retrieve Client from array and use it
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
or you could go thinking along the lines of a List. that way, you wouldn't have to know the number of elements up front.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
When you're filling up an array like this you need to maintain a count of how many Clients you have stored (initial value 0). The value of that count will also be the index of the next available array element. You just need to increment it each time you add a client.
(Like stultuske said, there are Java classes to do all this stuff for you, eg ArrayList, but your teacher probably wants you to learn about ordinary arrays first?)
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
its a bit simpler that that - you don't need to "loop to find number of indexes in array"
when count = 0, you store the first Client into array[0], increment count by 1.
Count is now 1, you store the next Client into array[1], increment count by 1.
Count is now 2, you store the next Client into array[2], increment count by 1.
see the pattern? :-)
So count tells you how many Clients you have, but because arrays are zero-based, that's also the right index to store the next Client in.
If you use an ArrayList you just call its add method and it does all that stuff for you (plus it makes the array bigger if you run out of available elements)
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
If something is declared in a method, its scope is that method - ie it cannot be seen from anywhere else. If you want to use variables in multiple methods you need to declare them outside any method (like clientID)
You have the manyClients array and is associated indexCount variable. These two always need to be used together so they should be declared together (in the same place). More seriously, if you make them instance variables (like clientID) then you will have a different one of them for every Client, which is not what you want. You want just one of each of those variables that is shared between all the instances of the class - remember static?
Finally your clientSave method is a bit of a mess. It gets some data related to a client, and then tries to retrieve the same data from the array (?).
What you need is that when you create a new Client you simply add that Client object to the array (then increment the count afterwards, not before).
I'm going out now, so I probably won't post again until tomorrow. Good luck!
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
The use of ArrayList has been suggested twice already in this thread. And it you want to retrieve by client id then you wouldn't use a clunky method like that (complete with redundant cast and no error handling), you would use a Map rather than a List.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073