File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes out of bounds exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "out of bounds exception" Watch "out of bounds exception" New topic
Author

out of bounds exception

andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
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

Joined: Oct 22, 2010
Posts: 181

Please put your code within CodeTag first..
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
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
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: out of bounds exception