Bookmark Topic Watch Topic
  • New Topic

Java Array of Strings  RSS feed

 
Brijesh Shah
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.....
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello,

Its clear that you are accessing element out of index bound.


strtoyid[0] = br.readLine();
strtoyname[1] = br.readLine();
strtoydesc[2] = br.readLine();

They are of length only one, so index 0 is allowed and you are trying to access the 1 and 2 elements. So error.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post the same question in more than one forums. Since this is not an advanced question, and because there is an on going discussion in your other topic I'm closing this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!