Results 1 to 3 of 3
  1. #1
    haritha is offline Member
    Join Date
    Jan 2016
    Rep Power

    Exclamation Throws error array out of bounds when run

    public class ItemDemo {

    public static void main(String []args)
    {double n;
    Item ob[]=new Item[5];
    ob[0]=new Item(1,"BOX",100,10);
    ob[1]=new Item(2,"CRAYON",200,20);
    ob[2]=new Item(3,"SCALE",10,2);
    ob[3]=new Item(4,"BAG",600,100);
    ob[4]=new Item(5,"SUITCASE",1500,200);
    ob[5]=new Item(6,"POUCH",200,60);
    System.out.println("Least price is" +ob[(int) n].getItemName());

    public static double getLeastPriceItem(Item ob[])
    {double p[]=new double[5];
    double d[]=new double[5];
    int i;
    p[i] = ob[i].getItemPrice();
    d[i] = p[i] - (ob[i].getItemDiscount() * 0.01 * p[i]);}
    double smallest = d[0];
    {if (d[i] < smallest)
    smallest = d[i];
    return smallest;}}}

    public class Item {
    private int itemId;
    private String itemName;
    private double itemPrice;
    private double itemDiscount;
    public Item(int itemId,String itemName,double itemPrice,double itemDiscount)
    public void setItemId(int itemId) {
    this.itemId = itemId;
    public double getItemPrice() {
    return itemPrice;
    public void setItemPrice(double itemPrice) {
    this.itemPrice = itemPrice;
    public double getItemDiscount() {
    return itemDiscount;
    public void setItemDiscount(double itemDiscount) {
    this.itemDiscount = itemDiscount;}
    public int getItemId()
    return itemId;
    public String getItemName() {
    return itemName;}}
    Last edited by haritha; 02-02-2016 at 08:26 AM. Reason: added the other class too

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Throws error array out of bounds when run

    The exception that is thrown will have a full stack trace showing what line in your code caused the problem, and the text will give additional information about the index used.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: Throws error array out of bounds when run

    What does this mean? Item ob[]=new Item[5]; This means you want an array of 5 items.

    Now count how many items you are inserting into the array....
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2013, 05:23 AM
  2. Out of Bounds Array...sometimes
    By lampposteffect in forum New To Java
    Replies: 12
    Last Post: 04-21-2011, 08:10 PM
  3. Replies: 4
    Last Post: 01-31-2011, 09:20 PM
  4. Array index out of bounds error
    By blackstyle18 in forum New To Java
    Replies: 3
    Last Post: 12-28-2010, 03:37 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 02:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts