Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

out of bounds exception

 
andrew cassato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been stuck on this line of code for a while now, it will compile fine but when I run it I get the error: "java.lang.ArrayIndexOutOfBoundsException: 3"
I am trying to input 3 different account numbers and compare them to 3 different account numbers in a file but it will stop me after entering the third account number... not really sure how much code I need to put in there so I will paste most of it here

//code

public static void main(String[] args) throws FileNotFoundException
{
Scanner console = new Scanner(System.in);

FileReader dataFile = new FileReader("accountsFile.txt");
Scanner fileIn = new Scanner(dataFile);

int totalAccount = 3;
String StringfileName = "";
String newAcc = "none";
StringfileName = "accountsFile.txt";
String[] accountNumber = new String[totalAccount];
String userOption = "Y";
String[] accountAmount = new String[3];
String acct1 = "";
String acct2 = "";
String acct3 = "";

userOption = JOptionPane.showInputDialog("Would you like to enter account numbers?");
String userOptionUpper = userOption.toUpperCase();


for (int i = 1; i <= 4; i+=1)
{
//accountNumber[i] = JOptionPane.showInputDialog("Please enter account #" + (i));
if(i==1)
{
accountNumber[i] = JOptionPane.showInputDialog("Please enter account #" + (i));
acct1 = fileIn.next();
System.out.println(acct1+" account read in");
}
if(i==2)
{
accountNumber[i] = JOptionPane.showInputDialog("Please enter account #" + (i));
acct2 = fileIn.next();
System.out.println(acct2+" account read in");
}
if(i==3)
{
accountNumber[i] = JOptionPane.showInputDialog("Please enter account #" + (i));
acct3 = fileIn.next();
System.out.println(acct3+" account read in");
}

}
//code

I am pretty new to java and 100% new to posting on forums so I apologize in advance for any dumb things that might be above.

Thanks in advance for any advice.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your code within CodeTag first..
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array of length 3 has elements 0, 1 and 2 - but not 3. That "3" is what the error message is complaining about.
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
andrew cassato wrote:I have been stuck on this line of code for a while now, it will compile fine but when I run it I get the error: "java.lang.ArrayIndexOutOfBoundsException: 3"


Java arrays are zero based -- so an index out of bounds at index 3 means that you are trying to access the fourth element.

Henry
 
andrew cassato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj gupta wrote:Please put your code within CodeTag first..


im not sure what that is...

based on what you said i thought initializing it to 2 would fix it, but that just changed where the error happened so i initialized it to 4 and that fixed it... though i still am not sure why.

thanks for your help
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
andrew cassato wrote:
based on what you said i thought initializing it to 2 would fix it, but that just changed where the error happened so i initialized it to 4 and that fixed it... though i still am not sure why.


The issue isn't how you initialized it -- it is how you used it. Your for loop starts from the second element to the fourth element. You need to modify your for-loop to start from the first element to the third element. IOWs, from element at index zero to element at index 2.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic