Board index » jbuilder » Creating and using vectors

Creating and using vectors


2004-05-25 06:46:58 PM
jbuilder18
Hi
Hope someone can help. I introduced a vector in my main class as follows:
<pre>
import java.io.*;
import java.util.*;
public class Loadsamoneybank {
private static BufferedReader In =
new BufferedReader(new InputStreamReader(System.in));
private Vector BankAccounts = new Vector();
</pre>
If you need the whole of my main class let me know! I then attempted to add a new bank account into this(a subclass of SAVINGS called EASYACCESS, I also have a superclass of ACCOUNT):
<pre>
public static void main(String[] args) {
int Option;
Account AccountObj = null;
Savings SavingsObj = null;
Easyaccess EasyaccessObj = null;
Highinterest HighinterestObj = null;
for (Option = 99; Option != 0;) {
/* Option cannot be 0 */
switch (Option) {
case 2:
SavingsObj = new Easyaccess(In);
BankAccounts.add(Easyaccess());
break;
</pre>
I then get compiler errors as follows:
<p>
C:\03032529\term3\8784\assign1\Loadsamoneybank.java:49: cannot resolve symbol
symbol : method Easyaccess ()
location: class Loadsamoneybank
BankAccounts.add(Easyaccess());
^
C:\03032529\term3\8784\assign1\Loadsamoneybank.java:49: non-static variable BankAccounts cannot be referenced from a static context
BankAccounts.add(Easyaccess());
^
.\Easyaccess.java:7: cannot resolve symbol
symbol : method Easyaccess ()
location: class Easyaccess
BankAccounts.add(Easyaccess());
^
.\Easyaccess.java:7: cannot resolve symbol
symbol : variable BankAccounts
location: class Easyaccess
BankAccounts.add(Easyaccess());
^
4 errors
Tool completed with exit code 1
<p>
I did try moving my vector to the main args bit as this has static but it didn't like that. Also tried adding static to the vector section but that was also unsuccessful
<p>
As you can probably see I am really lost with Vectors, not sure if I am constructing the vector in the right class(Main) or in the right place and then I don't know how to call it to add the information to it(which will come from ACCOUNT,SAVINGS & EASYACCESS classes).
<p>
Hope someone can help and in the mean time I will continue reading my books but they don't have an example of adding more than one element to a vector!
Have also just looked at he Java tutorial but never seem to find what I need in it :(
Look forward to any responses
Sonia
 
 

Re:Creating and using vectors

Changed my vector code to:
<pre>
private static Vector BankAccounts = new Vector();
</pre>
<p>
and it now compiles and also my line for adding details into the vector was altered to:
<pre>
</pre>
<p>
Hopefully i will be able to read objects in and out of my vector now....
"sonia" < XXXX@XXXXX.COM >wrote:
Quote

Hi

Hope someone can help. I introduced a vector in my main class as follows:
<pre>
import java.io.*;
import java.util.*;

public class Loadsamoneybank {

private static BufferedReader In =
new BufferedReader(new InputStreamReader(System.in));
private Vector BankAccounts = new Vector();
</pre>
If you need the whole of my main class let me know! I then attempted to add a new bank account into this(a subclass of SAVINGS called EASYACCESS, I also have a superclass of ACCOUNT):
<pre>
public static void main(String[] args) {
int Option;
Account AccountObj = null;
Savings SavingsObj = null;
Easyaccess EasyaccessObj = null;
Highinterest HighinterestObj = null;

for (Option = 99; Option != 0;) {

/* Option cannot be 0 */

switch (Option) {

case 2:
SavingsObj = new Easyaccess(In);
BankAccounts.add(Easyaccess());
break;
</pre>
I then get compiler errors as follows:
<p>
C:\03032529\term3\8784\assign1\Loadsamoneybank.java:49: cannot resolve symbol
symbol : method Easyaccess ()
location: class Loadsamoneybank
BankAccounts.add(Easyaccess());
^
C:\03032529\term3\8784\assign1\Loadsamoneybank.java:49: non-static variable BankAccounts cannot be referenced from a static context
BankAccounts.add(Easyaccess());
^
.\Easyaccess.java:7: cannot resolve symbol
symbol : method Easyaccess ()
location: class Easyaccess
BankAccounts.add(Easyaccess());
^
.\Easyaccess.java:7: cannot resolve symbol
symbol : variable BankAccounts
location: class Easyaccess
BankAccounts.add(Easyaccess());
^
4 errors

Tool completed with exit code 1
<p>
I did try moving my vector to the main args bit as this has static but it didn't like that. Also tried adding static to the vector section but that was also unsuccessful
<p>
As you can probably see I am really lost with Vectors, not sure if I am constructing the vector in the right class(Main) or in the right place and then I don't know how to call it to add the information to it(which will come from ACCOUNT,SAVINGS & EASYACCESS classes).
<p>
Hope someone can help and in the mean time I will continue reading my books but they don't have an example of adding more than one element to a vector!
Have also just looked at he Java tutorial but never seem to find what I need in it :(
Look forward to any responses

Sonia