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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java Array of Strings

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Screaming fools! It's nothing more than a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic