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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Light
    New Member
    • Feb 2008
    • 3

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

    Hi

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

    Code:
     
    
    import java.util.*;
    
    public class Exercise6 {
    
    	public static void main(String Args[]) {
    		
    		Scanner sc = new Scanner(System.in);
    		
    		System.out.println("Please enter 12 desired numbers that" +
    				" you wish to sort from a range 0-9.");
    		
    		int array[]= new int[12];
    		
    		for(int k=1; k<=array.length; k++) {
    			
    			System.out.print(k+ ":");
    			
    			int number = sc.nextInt();
    			
    			array[k] = number;
    		
    			if(k==12) {
    			  
    				for(int i=1; i<=array.length; i++) {
    					
    					System.out.print(array[i]);
    				}
    			}	
    			  
    			
    		}
    	}	
    		
          
    		
    		
    		
    		
    }
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Well, an ArrayIndexOutOf BoundsException 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?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Light
      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:

      Code:
       
      		int array[]= new int[12];
      		
      		for(int k=1; k<=array.length; k++) {
      			
      			System.out.print(k+ ":");
      			
      			int number = sc.nextInt();
      			
      			array[k] = number;
      Just count: if an array has twelve elements their index values are: 0, 1, 2 ... 11

      kind regards,

      Jos

      Comment

      • Light
        New Member
        • Feb 2008
        • 3

        #4
        Tnx a lot guys i ve fixed it

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          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:
          [CODE=Java]public class LoopExample {
          public static void main(String[] args) {
          int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
          for(int p : primes) {
          System.out.prin tln(p);
          }
          }
          }[/CODE]

          Comment

          • Pranali
            New Member
            • Nov 2010
            • 2

            #6
            Exception in thread "main" java.lang.Array IndexOutOfBound sException:

            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 InputStreamRead er(System.in));
            System.out.prin tln("Choose the alternative for the place you would like to visit from the following:");
            System.out.prin tln("1: Big Bazaar");
            System.out.prin tln("2: Sgs Mall");
            System.out.prin tln("3: Central");
            System.out.prin tln("4: Pune Central");
            String s=buf.readLine( );
            int a=Integer.parse Int(s);
            switch(a)
            {
            case 1:System.out.pr intln(" * Welcome to Big Bazaar *");
            break;
            case 2:System.out.pr intln(" ** Welcome to Sgs Mall **");
            break;
            case 3:System.out.pr intln(" * Welcome to Central *");
            break;
            case 4:System.out.pr intln(" ** Welcome to Pune Central **");
            break;
            default:System. out.println("Th is place does not exist in the alternatives");
            }
            for(int hu=0;hu<=5;hu++ )
            {
            System.out.prin tln(" Choose the alternative to which section would you like to go to from our mall");
            System.out.prin tln("1: Clothes");
            System.out.prin tln("2: Grocery");
            System.out.prin tln("3: Eatables");
            System.out.prin tln("4: Stationery");
            String t=buf.readLine( );
            int b=Integer.parse Int(t);
            switch(b)
            {
            case 1:System.out.pr intln("You are in the clothes section");
            break;
            case 2:System.out.pr intln("You are in the Grocery section");
            break;
            case 3:System.out.pr intln("You are in the eatable section");
            break;
            case 4:System.out.pr intln("You are in the Stationery section");
            break;
            default:System. out.println("So rry! This section does not exist in our mall");
            }
            if(b==1)
            {
            System.out.prin tln("Sr no"+"\t\t"+"ITE M"+"\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.prin tln((r+1)+"\t\t "+ar[r]+"\t\t\t\t\t"+a a[r]);
            }
            System.out.prin tln("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
            String see=buf.readLin e();
            int x=Integer.parse Int(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.prin tln("Would you like to but anything else from this section");
            String ss=buf.readLine ();
            if(ss.equalsIgn oreCase("Yes")= =true)
            continue;
            else
            break;
            }
            if(b==2)
            {
            System.out.prin tln("Sr no"+"\t\t"+"ITE M"+"\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.prin tln((q+1)+"\t\t "+ag[q]+"\t\t\t\t\t"+g g[q]);
            }
            System.out.prin tln("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
            String go=buf.readLine ();
            int y=Integer.parse Int(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.prin tln("Would you like to but anything else from this section");
            String xx=buf.readLine ();
            if(xx.equalsIgn oreCase("Yes")= =true)
            continue;
            else
            break;
            }
            if(b==3)
            {
            System.out.prin tln("Sr no"+"\t\t"+"ITE M"+"\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.prin tln((u+1)+"\t\t "+at[u]+"\t\t\t\t\t"+t t[u]);
            }
            System.out.prin tln("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
            String ew=buf.readLine ();
            int w=Integer.parse Int(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.prin tln("Would you like to but anything else from this section");
            String kk=buf.readLine ();
            if(kk.equalsIgn oreCase("Yes")= =true)
            continue;
            else
            break;
            }
            if(b==4)
            {
            System.out.prin tln("Sr no"+"\t\t"+"ITE M"+"\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.prin tln((n+1)+"\t\t "+ac[n]+"\t\t\t\t\t"+c c[n]);
            }
            System.out.prin tln("CHOOSE THE ITEM YOU WANT TO BUY FROM THE ABOVE ITEMS");
            String es=buf.readLine ();
            int bo=Integer.pars eInt(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.prin tln("Would you like to but anything else from this section");
            String jj=buf.readLine ();
            if(jj.equalsIgn oreCase("Yes")= =true)
            continue;
            else
            break;
            }
            System.out.prin tln("Would you like to go to any other section");
            String ss=buf.readLine ();
            if(ss.equalsIgn oreCase("Yes")= =true)
            continue;
            else
            break;

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              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

              Comment

              Working...