469,330 Members | 1,235 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12

3
Hi

I m getting this error while i m trying to run my program and i don't understand why that happens.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.util.*;
  4.  
  5. public class Exercise6 {
  6.  
  7.     public static void main(String Args[]) {
  8.  
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         System.out.println("Please enter 12 desired numbers that" +
  12.                 " you wish to sort from a range 0-9.");
  13.  
  14.         int array[]= new int[12];
  15.  
  16.         for(int k=1; k<=array.length; k++) {
  17.  
  18.             System.out.print(k+ ":");
  19.  
  20.             int number = sc.nextInt();
  21.  
  22.             array[k] = number;
  23.  
  24.             if(k==12) {
  25.  
  26.                 for(int i=1; i<=array.length; i++) {
  27.  
  28.                     System.out.print(array[i]);
  29.                 }
  30.             }    
  31.  
  32.  
  33.         }
  34.     }    
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. }
Feb 1 '08 #1
6 23846
Laharl
849 Expert 512MB
Well, an ArrayIndexOutOfBoundsException means you're trying to access an element of an array that doesn't exist. Now, arrays are 0-indexed, which means they have array.length elements, starting at 0. Thus, what must the index of the final element be and where does your code try to move beyond that element?
Feb 1 '08 #2
JosAH
11,448 Expert 8TB
I m getting this error while i m trying to run my program and i don't understand why that happens.
This is where it happens:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         int array[]= new int[12];
  3.  
  4.         for(int k=1; k<=array.length; k++) {
  5.  
  6.             System.out.print(k+ ":");
  7.  
  8.             int number = sc.nextInt();
  9.  
  10.             array[k] = number;
  11.  
Just count: if an array has twelve elements their index values are: 0, 1, 2 ... 11

kind regards,

Jos
Feb 1 '08 #3
Light
3
Tnx a lot guys i ve fixed it
Feb 1 '08 #4
BigDaddyLH
1,216 Expert 1GB
When you don't need the array index in the loop (which is the case at least half the time, I bet) you can use the new enhanced for loop syntax, introduced in 1.5:
Expand|Select|Wrap|Line Numbers
  1. public class LoopExample {
  2.     public static void main(String[] args) {
  3.         int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
  4.         for(int p : primes) {
  5.             System.out.println(p);
  6.         }
  7.     }
  8. }
http://java.sun.com/j2se/1.5.0/docs/...e/foreach.html
Feb 1 '08 #5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

The following program gives the above error

import java.io.*;
class Live
{
public static void main(String args[])throws IOException
{
float tot=0;
String ar[]=new String[25];
int aa[]=new int[25];
String ag[]=new String[15];
int gg[]=new int[15];
String at[]=new String[10];
int tt[]=new int[10];
String ac[]=new String[10];
int cc[]=new int[10];
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Choose the alternative for the place you would like to visit from the following:");
System.out.println("1: Big Bazaar");
System.out.println("2: Sgs Mall");
System.out.println("3: Central");
System.out.println("4: Pune Central");
String s=buf.readLine();
int a=Integer.parseInt(s);
switch(a)
{
case 1:System.out.println(" * Welcome to Big Bazaar *");
break;
case 2:System.out.println(" ** Welcome to Sgs Mall **");
break;
case 3:System.out.println(" * Welcome to Central *");
break;
case 4:System.out.println(" ** Welcome to Pune Central **");
break;
default:System.out.println("This place does not exist in the alternatives");
}
for(int hu=0;hu<=5;hu++)
{
System.out.println(" Choose the alternative to which section would you like to go to from our mall");
System.out.println("1: Clothes");
System.out.println("2: Grocery");
System.out.println("3: Eatables");
System.out.println("4: Stationery");
String t=buf.readLine();
int b=Integer.parseInt(t);
switch(b)
{
case 1:System.out.println("You are in the clothes section");
break;
case 2:System.out.println("You are in the Grocery section");
break;
case 3:System.out.println("You are in the eatable section");
break;
case 4:System.out.println("You are in the Stationery section");
break;
default:System.out.println("Sorry! This section does not exist in our mall");
}
if(b==1)
{
System.out.println("Sr no"+"\t\t"+"ITEM"+"\t\t\t\t\t"+"PRICE");
ar[0]="Any shirt from VAN HEUSEN brand";
ar[1]="Any shirt from LOUIS PHILIPPE brand";
ar[2]="Any shirt from WILLS LIFESTYLE brand";
ar[3]="Any party wear shirt PROVOGUE brand";
ar[4]="Any shirt from AUSTIN REED brand";
ar[5]="Blackberry flat front trousers(white)";
ar[6]="Blackberry flat front trousers(black)";
ar[7]="Any zodiac tie";
ar[8]="Life 3/4ths";
ar[9]="Life cargo";
ar[10]="Life Denim shorts";
ar[11]="Formal shirt for women from AUSTIN REED";
ar[12]="Ladies Denim Jeans";
ar[13]="Kashish Sarees";
ar[14]="Kashish Salwar Kameez";
ar[15]="BIBA Salwar Kameez";
ar[16]="Life Denim Shorts for Ladies";
ar[17]="Gini & Jony Top for girls";
ar[18]="Gini & Jony Dresses for girls";
ar[19]="Stop Girl Casual Dresses for girls";
ar[20]="Denim Shorts for girls below 7";
ar[21]="Tantra T-shirts for boys below 10";
ar[22]="Giny & Jony kids Shirt";
ar[23]="Giny & Jony Kids Jeans[boys]";
ar[24]="Giny & Jony Kids Jeans[girls]";
ar[25]="Socks for kids";
aa[0]=1599;
aa[1]=1499;
aa[2]=1399;
aa[3]=1499;
aa[4]=1499;
aa[5]=1595;
aa[6]=1695;
aa[7]=999;
aa[8]=999;
aa[9]=899;
aa[10]=699;
aa[11]=799;
aa[12]=999;
aa[13]=2395;
aa[14]=2295;
aa[15]=2295;
aa[16]=799;
aa[17]=599;
aa[18]=899;
aa[19]=549;
aa[20]=599;
aa[21]=225;
aa[22]=799;
aa[23]=999;
aa[24]=999;
aa[25]=135;
for(int r=0;r<=25;r++)
{
System.out.println((r+1)+"\t\t"+ar[r]+"\t\t\t\t\t"+aa[r]);
}
System.out.println("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
String see=buf.readLine();
int x=Integer.parseInt(see);
if(x==1)
tot+=aa[0];
else if(x==2)
tot+=aa[1];
else if(x==3)
tot+=aa[2];
else if(x==4)
tot+=aa[3];
else if(x==5)
tot+=aa[4];
else if(x==6)
tot+=aa[5];
else if(x==7)
tot+=aa[6];
else if(x==8)
tot+=aa[7];
else if(x==9)
tot+=aa[8];
else if(x==10)
tot+=aa[9];
else if(x==11)
tot+=aa[10];
else if(x==12)
tot+=aa[11];
else if(x==13)
tot+=aa[12];
else if(x==14)
tot+=aa[13];
else if(x==15)
tot+=aa[14];
else if(x==16)
tot+=aa[15];
else if(x==17)
tot+=aa[16];
else if(x==18)
tot+=aa[17];
else if(x==19)
tot+=aa[18];
else if(x==20)
tot+=aa[19];
else if(x==21)
tot+=aa[20];
else if(x==22)
tot+=aa[21];
else if(x==23)
tot+=aa[22];
else if(x==24)
tot+=aa[23];
else if(x==25)
tot+=aa[24];
else
tot+=aa[25];
System.out.println("Would you like to but anything else from this section");
String ss=buf.readLine();
if(ss.equalsIgnoreCase("Yes")==true)
continue;
else
break;
}
if(b==2)
{
System.out.println("Sr no"+"\t\t"+"ITEM"+"\t\t\t\t\t"+"PRICE");
ag[0]="1kg Potatoes";
ag[1]="1kg Tomatoes";
ag[2]="1kg Onion";
ag[3]="1kg Apples";
ag[4]="1kg Cabbage";
ag[5]="1kg Cauliflower";
ag[6]="1dozen Bannanas";
ag[7]="1dozen Chikoos";
ag[8]="1kg Watermelon";
ag[9]="1kg Strawberries";
ag[10]="1kg Sugar";
ag[11]="1kg Red Label Tea";
ag[12]="1kg Tata Salt";
ag[13]="5 Lemons";
ag[14]="1kg Rice";
ag[15]="1kg Wheat";
gg[0]=15;
gg[1]=20;
gg[2]=30;
gg[3]=80;
gg[4]=20;
gg[5]=40;
gg[6]=40;
gg[7]=60;
gg[8]=65;
gg[9]=150;
gg[10]=30;
gg[11]=200;
gg[12]=10;
gg[13]=10;
gg[14]=40;
gg[15]=25;
for(int q=0;q<=15;q++)
{
System.out.println((q+1)+"\t\t"+ag[q]+"\t\t\t\t\t"+gg[q]);
}
System.out.println("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
String go=buf.readLine();
int y=Integer.parseInt(go);
if(y==1)
tot+=gg[0];
else if(y==2)
tot+=gg[1];
else if(y==3)
tot+=gg[2];
else if(y==4)
tot+=gg[3];
else if(y==5)
tot+=gg[4];
else if(y==6)
tot+=gg[5];
else if(y==7)
tot+=gg[6];
else if(y==8)
tot+=gg[7];
else if(y==9)
tot+=gg[8];
else if(y==10)
tot+=gg[9];
else if(y==11)
tot+=gg[10];
else if(y==12)
tot+=gg[11];
else if(y==13)
tot+=gg[12];
else if(y==14)
tot+=gg[13];
else if(y==15)
tot+=gg[14];
else
tot+=gg[15];
System.out.println("Would you like to but anything else from this section");
String xx=buf.readLine();
if(xx.equalsIgnoreCase("Yes")==true)
continue;
else
break;
}
if(b==3)
{
System.out.println("Sr no"+"\t\t"+"ITEM"+"\t\t\t\t\t"+"PRICE");
at[0]="Pani Puri";
at[1]="Sev Puri";
at[2]="Ragda Puri";
at[3]="Club sandwich";
at[4]="Basket Chaat";
at[5]="Pav Bhaji";
at[6]="Plain Dosa";
at[7]="Masala Dosa";
at[8]="Idli Chatni";
at[9]="Veg Pizza";
at[10]="Veg Burger";
tt[0]=25;
tt[1]=25;
tt[2]=30;
tt[3]=20;
tt[4]=45;
tt[5]=60;
tt[6]=75;
tt[7]=95;
tt[8]=80;
tt[9]=125;
tt[10]=25;
for(int u=0;u<=10;u++)
{
System.out.println((u+1)+"\t\t"+at[u]+"\t\t\t\t\t"+tt[u]);
}
System.out.println("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
String ew=buf.readLine();
int w=Integer.parseInt(ew);
if(w==1)
tot+=tt[0];
else if(w==2)
tot+=tt[1];
else if(w==3)
tot+=tt[2];
else if(w==4)
tot+=tt[3];
else if(w==5)
tot+=tt[4];
else if(w==6)
tot+=tt[5];
else if(w==7)
tot+=tt[6];
else if(w==8)
tot+=tt[7];
else if(w==9)
tot+=tt[8];
else if(w==10)
tot+=tt[9];
else
tot+=tt[10];
System.out.println("Would you like to but anything else from this section");
String kk=buf.readLine();
if(kk.equalsIgnoreCase("Yes")==true)
continue;
else
break;
}
if(b==4)
{
System.out.println("Sr no"+"\t\t"+"ITEM"+"\t\t\t\t\t"+"PRICE");
ac[0]="Navneet A4 size notebooks(200 pgs)";
ac[1]="Navneet fullscape notebooks(200 pgs)";
ac[2]="Navneet Journals(200 pgs)";
ac[3]="Navneet small square maths notebook(200 pgs)";
ac[4]="Reynolds pen";
ac[5]="Add Gel pen";
ac[6]="Journal Sheets(1 dozen)";
ac[7]="Graph Paper(1 dozen)";
ac[8]="400 pages Diary";
ac[9]="Apsara Pencil Box";
ac[10]="Geometry Box";
cc[0]=15;
cc[1]=25;
cc[2]=49;
cc[3]=25;
cc[4]=15;
cc[5]=10;
cc[6]=6;
cc[7]=6;
cc[8]=75;
cc[9]=25;
cc[10]=120;
for(int n=0;n<=10;n++)
{
System.out.println((n+1)+"\t\t"+ac[n]+"\t\t\t\t\t"+cc[n]);
}
System.out.println("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
String es=buf.readLine();
int bo=Integer.parseInt(es);
if(bo==1)
tot+=cc[0];
else if(bo==2)
tot+=cc[1];
else if(bo==3)
tot+=cc[2];
else if(bo==4)
tot+=cc[3];
else if(bo==5)
tot+=cc[4];
else if(bo==6)
tot+=cc[5];
else if(bo==7)
tot+=cc[6];
else if(bo==8)
tot+=cc[7];
else if(bo==9)
tot+=cc[8];
else if(bo==10)
tot+=cc[9];
else
tot+=cc[10];
System.out.println("Would you like to but anything else from this section");
String jj=buf.readLine();
if(jj.equalsIgnoreCase("Yes")==true)
continue;
else
break;
}
System.out.println("Would you like to go to any other section");
String ss=buf.readLine();
if(ss.equalsIgnoreCase("Yes")==true)
continue;
else
break;
Nov 23 '10 #6
Dheeraj Joshi
1,123 Expert 1GB
Array indexes in Java is zero based. So if you declare a array of size 25. You can use only 0-24 element. There is no 25th index. 25th element present in 24th index.

And i suggest you to modify the code littler bit.

Regards
Dheeraj Joshi
Nov 24 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by tiyaramunna | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.