HI I have a code here:
When I enter the Toy Identificationof the toy1:
S01 and then hit enter it should ask me another question as
Enter the Toy Nameof the toy1:
But instead it goes to the next line blank withiout the above 2nd question:
Also I get the follwoing error:
Exception in
thread "main" java.lang.ArrayIndexOutOfBoundsException
at ToyCollection.main(ToyCollection.java:41)
The complete code is here:
import java.io.*;
class Toy
{
String strid, strname, strdesc;
float fltprice;
short shtquantity;
Toy(String i, String n, String d, float p, short q)
{
strid=i;
strname=n;
strdesc=d;
fltprice=p;
shtquantity=q;
}
float TotalPrice()
{
return fltprice * shtquantity;
}
}class ToyCollection
{
public static void main(String[] args) throws IOException
{
int intnum;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of toys information to be entered: ");
intnum = Integer.parseInt(br.readLine());
Toy obj[] = new Toy[intnum];
String strtoyid[] = new String[1];
String strtoyname[] = new String[1];
String strtoydesc[] = new String[1];
float flttoyprice[] = new float[1];
short shttoyquantity[] = new short[1];
String strtoy[] = {"Toy Identification", "Toy Name", "Toy Description", "Toy Price", "Quantity in hand"};
for (int inti=0; inti<intnum; inti++)
{
for (int intj=0; intj<5; intj++)
{
System.out.println("Enter the "+strtoy[intj]+"of the toy"+(inti+1)+ ":");
strtoyid[0] = br.readLine();
strtoyname[1] = br.readLine();
strtoydesc[2] = br.readLine();
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 (ArithmeticException e)
{
System.out.println(e);
}
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 (ArithmeticException e)
{
System.out.println(e);
}
}
obj[inti] = new Toy(strtoyid[0],strtoyname[1],strtoydesc[2],flttoyprice[3],shttoyquantity[4]);
}
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);
}
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]);
}
}
Please assist.....