473,770 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 24256
Laharl
849 Recognized Expert Contributor
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?
Feb 1 '08 #2
JosAH
11,448 Recognized Expert MVP
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 New Member
Tnx a lot guys i ve fixed it
Feb 1 '08 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
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
Pranali
2 New Member
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;
Nov 23 '10 #6
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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

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

Similar topics

0
3010
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's webpage and installed it using SGI's swmgr application. The installation was very straight forward and there were no errors when I installed the package. Then I ran /usr/java2/bin/javac SelectSockets.java to make the SelectSockets.class file.
22
41040
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to Queue" run Producent queue1 queue2 queue3 queue4 maybe you will se something i dont see
3
6444
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
9
3285
by: tiyaramunna | last post by:
I am trying to configure my system with Java program just to practice on the coding....when i compile a test.java program i am able to see the class file but i cant run the program ... I am getting the following error Exception in thread "main" java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at java.net.URLClassLoader$1.run(Unknown Source) at...
4
14730
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar" -jar CurrentStrobe.jar Exception in thread "main"...
3
7659
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at sortmergejoin.MergeJoin.Field(MergeJoin.java:204) at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84) at sortmergejoin.MergeJoin.<init>(MergeJoin.java:34) at sortmergejoin.Main.main(Main.java:24) Java Result: 1"
1
10099
by: rajujrk | last post by:
Hai All, I am Having a problem in the following... import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.io.FileUtils; import java.io.IOException;
1
3721
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); EmployeeService es=(EmployeeService)beans.getBean("employeeService");
1
1862
by: Buena Velasco | last post by:
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other. import java.util.*; public class RoundRobin { static Scanner console = new Scanner (System.in); public static void main (Stringargs)throws Exception {
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.