我试图理解为什么我的数组不断返回为null或超出范围。我必须使用来自文本字段的用户输入来填充数组。有两种方法:一种用于检查密码,另一种用于存储密码。
//instance variables
Account [] myAccount = new Account[10];
int lastAccountInteger = -1;
final int MAX_ACCOUNTS_INTEGER = 9;
`enter code here`public void processNewAccount()
{
//Create local variables
int pinCheckInteger;
String firstNameString = "", lastNameString = "", pinString;
firstNameString = firstNameTextField.getText();
lastNameString = lastNameTextField.getText();
pinString = newPinTextField.getText();
lastAccountInteger++;
if(lastAccountInteger == 0)
{
if(firstNameString.equals("") && lastNameString.equals(""))
{
AlertDialog("Account Name", "Name on account", "Enter your name");
firstNameTextField.requestFocus();
}
if(pinString.equals(""))
{
AlertDialog("Pin", "Pin number is blank", "Enter a Pin");
newPinTextField.requestFocus();
}
else
{
if(checkPin(pinString) == 0)
{
AlertDialog("Pin", "Pin number does not exist", "Enter a Pin");
newPinTextField.selectAll();
newPinTextField.requestFocus();
}
else
{
pinCheckInteger = checkPin(pinString);
displayAllAccountInfo(pinCheckInteger);
}
}
}
else
{
AlertDialog("Pin", "Pin", "pin");
}
}//end of processNewAccount method
public int checkPin(String checkPinString)
{
boolean checkPinBoolean = false;
int indexInteger = 0;
String pinString;
while(!checkPinBoolean && indexInteger <= lastAccountInteger)
lastAccountInteger++ )
{
//Stores the item of the myAccount array that is being checked
pinString = String.valueOf(myAccount[lastAccountInteger].getPin());
//Checking if the user input and the item are the same
if(pinString.length() == 4)
{
checkPinString.equals(pinString);
//Assigns to true
checkPinBoolean = true;
}
else
{
//Add to one
indexInteger++;
}
}
//If the boolean is true then return the indexInteger
if(checkPinBoolean == false)
{
indexInteger = -1;
}
//Return the position in the array where the pin matches or not
return indexInteger;
}
我正试图存储图钉并取回它,然后递增到下一个元素,并对10个帐户执行相同的操作。