Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes ArrayOutOfBoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayOutOfBoundException" Watch "ArrayOutOfBoundException" New topic
Author

ArrayOutOfBoundException

Brijesh Shah
Ranch Hand

Joined: Aug 08, 2006
Posts: 34
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....
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Brijesh Shah
Ranch Hand

Joined: Aug 08, 2006
Posts: 34
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

Joined: Apr 25, 2005
Posts: 809
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 ]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Indeed. Off to "Java in General (Beginner)" for this topic.


[Jess in Action][AskingGoodQuestions]
 
 
subject: ArrayOutOfBoundException
 
Similar Threads
Java Array of Strings
Scanners?! Where?!
arun ameriya
Complicated Array Problem
Java Array of Strings