0

I have been fiddling about with this code, and cannot work out how to create and add to an array without incurring some error one way or the other. This is the closest I have come to getting it to work.

I have tried creating the array within my first loop, but I need to access it later in the program.

String  userInput;
int i=0; 
String damages [] = new String [i];
double repairCost [] = new double [i];

int j=0;
String infringements [] = new String [j];
double fines [] = new double [j];

double repairCostTotal = 0.00;
double finesTotal = 0.00;
double finalHire = 0.00;  

do
{
    System.out.print("Damage: D, Infringements: F, "Exit: X");
    userInput = stdin.readLine();

    if (userInput.equalsIgnoreCase("D"))
    {

        System.out.println("Damage description:");
        damages[i] = stdin.readLine();  //<<<<--line of error

        System.out.println("Repair Costs:");
        repairCost [i] = Double.parseDouble(stdin.readLine());
        repairCostTotal = repairCostTotal + repairCost [i];
        i=i+1;
    }
    else if (userInput.equalsIgnoreCase("F"))
    {

        System.out.println("Infringement type:");
        infringements[j] = stdin.readLine();

        System.out.println("Fine:");
        fines [j] = Double.parseDouble(stdin.readLine());
        finesTotal = finesTotal + fines [j];
        j=j+1;
     }
    else if (!userInput.equalsIgnoreCase("X"))
    {
        System.out.print("Enter a valid Selection:");
    }
}

while (!userInput.equalsIgnoreCase("x"));


System.out.println("Damage Repair Details: ");

for ( i= 0; i < damages.length; i++)
    System.out.println("- " + damages [i] + " ($" + repairCost[i] + ")");

System.out.printf("Damage Repair Total: %10.2f %n", repairCostTotal);


System.out.println("Traffic Infringement Details:");

for (j= 0; j < fines.length; j++)
    System.out.println("- " + infringements [i] +  " ($" +  fines[j] + ")");

System.out.printf("Traffic Fine Total:  %10.2f %n", finesTotal);

I am getting this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at CarHire.main(CarHire.java:106)

I'm sure it's a simple principle I am missing, any help is appreciated.

2
  • You may try ArrayList for dynamic arrays. Commented Sep 25, 2013 at 14:04
  • ArrayList have some similar to Array implamentation. It storing values in ordnary Array and extend it's length when it is needed. Or you can create on your own new Array and copy values to it. Commented Sep 25, 2013 at 14:07

4 Answers 4

4

Use Java ArrayList to create arrays of dynamic size.

Check out this documentation

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you use arrays initialized to size 0 - your code:

int j=0;
String infringements [] = new String [j];
double fines [] = new double [j];

You have to initialize it to the size of the input (i.e. j > 0) or use List which have dynamic size in case you don't know the input size ahead.

Comments

2

Your problem is that you're creating an array of 0 length, meaning you can't put anything in it. Then you try to put something at index 0.

Secondly, there are no arrays of "unknown" size. The size is always known at array creation time. If you want dynamic arrays, you'll need to use an ArrayList or other Collection.

Comments

2
int j=0;
String infringements [] = new String [j];

You are creating an array of size 0...

If you don't know the size, use a list such as ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.