Results 1 to 7 of 7
  1. #1
    spk
    spk is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException

    Hello :-)
    So, I've made this program for car rental services and when I execute the program, an error saying ArrayIndexOutOfBounds is displayed at some point.
    The program is given below -




    import java.io.*;

    class car_rentals

    {

    public static void main () throws IOException

    {

    DataInputStream in= new DataInputStream (System.in);

    String car_model = new String ();int ch;

    int flag=0; int dotemp=0;

    System.out.println("Welcome to Star Car Rental Application Services");

    int temp1=0;

    int tempstatus;

    int charges=0;

    int n_o_d=1;

    String tempS = new String();

    System.out.println("Please enter the name of your company");

    String comp_name= in.readLine();

    System.out.println("Please enter the total number of cars offered for rental");

    int n= Integer.parseInt(in.readLine());

    String car_name[]= new String [n];

    String cust_name []= new String [n];

    int status[]= new int [n];

    int price [] =new int [n];

    int S; int depo;

    System.out.println("Please enter the following in the form of a list for each car-");
    System.out.println("Car Model Name");
    System.out.println("Rental Charges for the car");
    System.out.println("If car is available enter 1, if unavailable enter 2");



    for (int i=0;i<n;i++)

    {

    car_name[i]=in.readLine();

    price[i]=Integer.parseInt(in.readLine());

    status[i]= Integer.parseInt(in.readLine());



    }

    System.out.println("Enter the normal deposit for renting a car");
    depo= Integer.parseInt(in.readLine());

    for(int i=0;i<n;i++)

    {

    for(int j=0;j<n;j++)

    {


    if(price[j]>price[j+1]) // the problem comes at this point
    {
    temp1 = price[j];

    price[j] = price[j+1];

    price[j+1] = temp1 ;

    tempS=car_name[j];

    car_name[j]=car_name[j+1];

    car_name[j+1]=tempS;

    tempstatus=status[j];

    status[j]=status[j+1];

    status[j+1]=tempstatus;

    }}}

    do

    {

    System.out.println("Menu:(please enter appropriate number)");

    System.out.println("1.Search availibilty of model");

    System.out.println("2.Rent a Car");

    System.out.println("3.Quit");

    ch= Integer.parseInt(in.readLine());

    switch(ch){};

    {

    Case1:

    for(int i=0;i<n;i++)

    {

    if(status[i]==1)

    {

    System.out.println("The model is available for rent");

    System.out.println("Please enter customer name");

    cust_name[i]= in.readLine();

    System.out.println("Please enter no. of days car is to be rented");

    n_o_d=Integer.parseInt(in.readLine());

    charges=n_o_d*price[i];

    System.out.println("The total charges are"+ charges);

    }

    else

    System.out.println("The car is unavailable;please try again");break;

    }

    Case2: for(int i=0;i<n;i++)

    {

    System.out.println("The model name is " + car_name[i]);

    System.out.println("The rental charges per day are " + price[i]);

    if(status[i]==1)

    System.out.println("Model is available");

    else

    System.out.println("Model is unavailable");

    }//end of for loop

    System.out.println("Enter your car model of choice");

    car_model=in.readLine();

    for(int i=0;i<n;i++)

    {

    if (car_model==car_name[i])

    {

    if(status[i]!=1)

    {

    System.out.println("This car can not be rented as it is unavailable");

    }

    flag=1;

    }

    if(flag==0)

    System.out.println("Car model not found.Please try again with a different model");

    System.out.println("Enter no. of days to be rented");

    int nd=Integer.parseInt(in.readLine());

    System.out.println("The rental charges are "+ (price[i]*nd));

    System.out.println("Deposit for rental " + depo);

    System.out.println("Total cost = rental charge + deposit amount " + ((price[i]*nd)+depo));

    break;}

    Case3: break;

    }

    }while(n>0);



    }

    }



    Thanks in advance :-)
    Last edited by spk; 09-02-2013 at 05:51 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: ArrayIndexOutOfBoundsException

    If an array has 'n' elements, their index values are 0, 1, 2, ... ,n-1; carefully check the line you have boldfaced and see what index values your program is trying to use.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,406
    Rep Power
    20

    Default Re: ArrayIndexOutOfBoundsException

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    spk
    spk is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException

    for(int i=0;i<n;i++)

    {

    for(int j=0;j<n-1;j++)

    {


    if(price[j]>price[j+1])



    Have I caught on my mistake?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: ArrayIndexOutOfBoundsException

    The learning moment here is when you can explain that for yourself, not when someone tells you if you succeeded or not.

    Prove that what you changed is correct. A quick way to do that is to see that the code does in fact do what it is supposed to do (IE. it does not crash and produces the correct output). If it doesn't do what you think it should do, try adding System.out.println() statements to print out the values of all the different variables you are using.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    spk
    spk is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException

    It indeed does work, thank you for your help.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: ArrayIndexOutOfBoundsException

    Note that testing the stuff and the tests are successful, doesn't prove anything. Only if those tests fail you have proved the opposite (or your tests are incorrect ;-).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. ArrayIndexOutOfBoundsException: 10 ??
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 06:56 PM
  2. ArrayIndexOutOfBoundsException: 0
    By mxsar in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 10:59 PM
  3. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 06:50 PM
  4. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  5. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM

Posting Permissions

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