Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Array Problem

Status
Not open for further replies.

asm1

Technical User
Oct 9, 2003
77
GB
I am having problems accessing the following array can some one please show me how to do this (newbie). I am trying to access the array from a class called AccGui and the array is in a class called AddAccount. The array is created as shown
Code:
private void makeAccount()
{
	String accNum;
	String accBal;
	
	accBal=tBalance.getText();
	accNum=tAccNum.getText();
	int intAccNum = Integer.parseInt(accNum);
	newAccountNum++;
	float flAccBal= Float.parseFloat(accBal);
		
	anAccount=new SavingsAccount(intAccNum, flAccBal, 0.05f);
	sa[num] = anAccount;//i makes it italic so i am putting num
	i++;
}

then I created the following AddAccount to access the array from different classes
Code:
public SavingsAccount[]getSavingsAccountArr(int num)
{
        SavingsAccount[] tempList=new SavingsAccount[10];
	for (int i=0;i<10;++i)
	{
	    tempList[num] =sa[num];//should be i
	}
	return tempList;

i then call this from AccGui with the following

Code:
public float getSavingsAccountBal(int num)
{
	float anynum;
	SavingsAccount[] tempList=new SavingsAccount[10];
	//for (int i=0;i<10;++i)
	anynum=sa[num];
	//getSavingsAccountArr();
	//anynum= accounts.getSavingsAccountBal;
		
	return anynum;
}

as you can see i have tried different ways [evil] and these are just a few but i have not had any luck. all comments welcome as i have lost the plot now, thx
 
So that is very confusing.
Sometimes you use 'i', sometimes 'num'.
We don't know what it is, a 'SavingsAccount'.

In 'getSavingsAcountBal' 'tempList' is created but never used.
Even not in the commented Lines.

The second method seems to copy 10 values from one array to another one. But what is the use of 'num' if you meant to use 'i' instead of 'num' in the loop?
If you use num, you will copy 10 times the same value to the same destination, which is suspicious.

I think you are talking of Accounts and want to store a number of Accounts in a Collection.
Arrays are popular to new Java-programmers, because they are known from different languages.
Their drawback is, you need to know their size by compiletime.
Perhaps Vector could be a better solution to you.

And Account looks like a class on it's own.

public class Account
{
public long balance;
public long percent;

public Account (long b, long p)
{
balance = b;
percent = p;
}
// additional things
}

You should not use float for arithmetic calculations on money, because from the numberformat of float (and double), you loose precision by big numbers.
Calculate all money in cent!
The second issue is, not to store the index in the data-object.
The index is stored by the Collection.

You use your Account like this:

Account acc = new Account (300, 5);
Vector accounts = new Vector ();

accounts.add (acc);

// get the first element of the collection
// which has index=0
Account a0 = accounts.get (0);

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top