• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ArrayOutOfBoundException

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a code here:

import java.io.*;
class Toy
{
String strid, strname, strdesc;
float fltprice;
short shtquantity;
// Declaring the parameterized constructor
Toy(String i, String n, String d, float p, short q)
{
strid=i;
strname=n;
strdesc=d;
fltprice=p;
shtquantity=q;
}// closing constructor Toy
float TotalPrice()
{
return fltprice * shtquantity;
}
}// closing class Toy
class ToyCollection
{
public static void main(String[] args) throws IOException
{
int intnum;
// Read text from a character-input stream
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of toys information to be entered: ");
// Read a line of data from the file as a Integer object
intnum = Integer.parseInt(br.readLine());
Toy obj[] = new Toy[intnum];
String strtoyid[] = new String[8];
String strtoyname[] = new String[12];
String strtoydesc[] = new String[15];
float flttoyprice[] = new float[5];
short shttoyquantity[] = new short[3];
// Defining array of strings
String strtoy[] = {"Toy Identification", "Toy Name", "Toy Description", "Toy Price", "Quantity in hand"};
for (int inti=0; inti<intnum; inti++)
{
System.out.println("Enter the Toy Identification of the toy"+(inti+1)+ ":");
strtoyid[0] = br.readLine();
System.out.println("Enter the Toy Name of the toy"+(inti+1)+ ":");
strtoyname[1] = br.readLine();
System.out.println("Enter the Toy Description of the toy"+(inti+1)+ ":");
strtoydesc[2] = br.readLine();
System.out.println("Enter the Toy Price of the toy"+(inti+1)+ ":");
try
{
flttoyprice[3] = Float.parseFloat(br.readLine());
if (flttoyprice[3]<=0)
{
System.out.println("Please enter only positive value of price of the toy.");
}
}
catch (Exception e) {}
System.out.println("Enter the Total Quantity of the toy"+(inti+1)+ ":");
try
{
shttoyquantity[4] = Short.parseShort(br.readLine());
if (shttoyquantity[4]<=0)
{
System.out.println("Please enter only positive value of quantity of the toy.");
}
}
catch (Exception e) {}
obj[inti] = new Toy(strtoyid[0],strtoyname[1],strtoydesc[2],flttoyprice[3],shttoyquantity[4]);
}// end for
float flttoytotprice;
for (int inti=0; inti<intnum; inti++)
{
flttoytotprice=obj[inti].TotalPrice();
System.out.println("The Total Price of the toy" +(inti+1)+ "is: "+flttoytotprice);
}// end for
System.out.println("Toy Identification: "+strtoyid[0]);
System.out.println("Toy Name: "+strtoyname[1]);
System.out.println("Toy Description: "+strtoydesc[2]);
System.out.println("Toy Price: "+flttoyprice[3]);
System.out.println("Quantity in hand: "+shttoyquantity[4]);
}// closing main
}// closing class ToyCollection


In the above code I get an error after typing the total quantity of the toy:
Error: ArrayOutOfBoundException
Please assist....
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Brijesh Shah:
Hi

I have a code here:

import java.io.*;
class Toy
{
String strid, strname, strdesc;
float fltprice;
short shtquantity;
// Declaring the parameterized constructor
Toy(String i, String n, String d, float p, short q)
{
strid=i;
strname=n;
strdesc=d;
fltprice=p;
shtquantity=q;
}// closing constructor Toy
float TotalPrice()
{
return fltprice * shtquantity;
}
}// closing class Toy
class ToyCollection
{
public static void main(String[] args) throws IOException
{
int intnum;
// Read text from a character-input stream
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of toys information to be entered: ");
// Read a line of data from the file as a Integer object
intnum = Integer.parseInt(br.readLine());
Toy obj[] = new Toy[intnum];
String strtoyid[] = new String[8];
String strtoyname[] = new String[12];
String strtoydesc[] = new String[15];
float flttoyprice[] = new float[5];
short shttoyquantity[] = new short[3];
// Defining array of strings
String strtoy[] = {"Toy Identification", "Toy Name", "Toy Description", "Toy Price", "Quantity in hand"};
for (int inti=0; inti<intnum; inti++)
{
System.out.println("Enter the Toy Identification of the toy"+(inti+1)+ ":");
strtoyid[0] = br.readLine();
System.out.println("Enter the Toy Name of the toy"+(inti+1)+ ":");
strtoyname[1] = br.readLine();
System.out.println("Enter the Toy Description of the toy"+(inti+1)+ ":");
strtoydesc[2] = br.readLine();
System.out.println("Enter the Toy Price of the toy"+(inti+1)+ ":");
try
{
flttoyprice[3] = Float.parseFloat(br.readLine());
if (flttoyprice[3]<=0)
{
System.out.println("Please enter only positive value of price of the toy.");
}
}
catch (Exception e) {}
System.out.println("Enter the Total Quantity of the toy"+(inti+1)+ ":");
try
{
shttoyquantity[4] = Short.parseShort(br.readLine());
if (shttoyquantity[4]<=0)
{
System.out.println("Please enter only positive value of quantity of the toy.");
}
}
catch (Exception e) {}
obj[inti] = new Toy(strtoyid[0],strtoyname[1],strtoydesc[2],flttoyprice[3],shttoyquantity[4]);
}// end for
float flttoytotprice;
for (int inti=0; inti<intnum; inti++)
{
flttoytotprice=obj[inti].TotalPrice();
System.out.println("The Total Price of the toy" +(inti+1)+ "is: "+flttoytotprice);
}// end for
System.out.println("Toy Identification: "+strtoyid[0]);
System.out.println("Toy Name: "+strtoyname[1]);
System.out.println("Toy Description: "+strtoydesc[2]);
System.out.println("Toy Price: "+flttoyprice[3]);
System.out.println("Quantity in hand: "+shttoyquantity[4]);
}// closing main
}// closing class ToyCollection


In the above code I get an error after typing the total quantity of the toy:
Error: ArrayOutOfBoundException
Please assist....



The number of elements specified at the time of creation of short array shttoyquantity is 3 and you are assigning some value at the index position 4

To make rest of code work change the length of shttoyquantity array to 5.

Naseem
[ September 09, 2006: Message edited by: Naseem Khan ]
 
Brijesh Shah
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naseem,

So is that mean that the value we enter in the square bracket is the position of the Quantity with data type short and not the actual number of digits entered by the user ??
Thanks for your reply...
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Brijesh Shah:
Hi Naseem,

So is that mean that the value we enter in the square bracket is the position of the Quantity with data type short and not the actual number of digits entered by the user ??
Thanks for your reply...



Yes at the time of access, you need to provide the index position that must be less than array length.

Since java arryas are of fixed length. They can't grow unlike Collections. So if at the time of creation, length of array is specified as 3, then we can allocate at its index position 0, 1 and 2 not at 4 as you did.

Naseem
[ September 09, 2006: Message edited by: Naseem Khan ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code within the [CODE] tag so that its easy to read. Also... post questions of ease at the beginner or intermediate forums.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. Off to "Java in General (Beginner)" for this topic.
    Bookmark Topic Watch Topic
  • New Topic