Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm getting this weird exception, I don't really understand why.. I tried debugging and found out that it goes wrong when running:

opt[i][j] = Double.POSITIVE_INFINITY; 

and when i == 0 and j == 1, but this shouldn't happen since in this case opt is a 9x6 matrix.

This is my code:

public class Versie3 {

    private int desCap;
    private int currentCap;
    private int maxCap;
    private int timeSlot;
    private static ArrayList<Double> prices;
    private double[][] opt = new double[timeSlot + 1][maxCap + 1];

    public Versie3() throws FileNotFoundException {

    }

    public void readInput(String s) throws FileNotFoundException 
    {
        FileReader fr = new FileReader(s);
        Scanner sc = new Scanner(fr);

        timeSlot = sc.nextInt();
        maxCap = sc.nextInt();
        currentCap = sc.nextInt();
        desCap = sc.nextInt();
        prices = new ArrayList<Double>(timeSlot);

        while (sc.hasNextDouble()) {
            prices.add(sc.nextDouble());

        }
    }

    public double calculateOptimal() 
    {
        for (int i = 0; i <= timeSlot; i++) 
        {
            for (int j = 0; j <= maxCap; j++) 
            {
                if (i == 0) 
                {
                    if (j != desCap) 
                    {

                        opt[i][j] = Double.POSITIVE_INFINITY; // <--here it goes Wrong!
                    } 
                    else 
                    {
                        opt[i][j] = 0;
                    }
                } 
                else if (j == 0) 
                {
                    opt[i][j] = Math.min(opt[i - 1][j],
                            opt[i - 1][j + 1]
                                    - prices.get(i-1));
                } 
                else if (j == maxCap) 
                {
                    opt[i][j] = Math.min(opt[i - 1][j],
                            opt[i - 1][j - 1]
                                    + prices.get(i-1));
                } 
                else 
                {
                    opt[i][j] = Math.min(Math.min(opt[i - 1][j],
                    opt[i - 1][j - 1]
                    + prices.get(i - 1)),opt[i - 1][j + 1]- prices.get(i-1));
                }
            }
        }
        return opt[timeSlot][currentCap];
    }

    public static void main(String[] args) throws FileNotFoundException {
        Versie3 v3 = new Versie3();
        v3.readInput("input.txt");
        System.out.println("prices: " + prices.toString());
        System.out.println("timeSlot: " + v3.timeSlot);
        System.out.println("maxCap: " + v3.maxCap);
        System.out.println("currentCap: " + v3.currentCap);
        System.out.println("desCap: " + v3.desCap);
        //System.out.println("minimum cost: "+v3.calculateOptimal());
        System.out.println(v3.prices.size());

    }

}

And this is the input file I'm reading:

8 5 2 5
2.2 3 5 6.5 5 5 3 1.8

In this case:

timeSlot = 8
maxCap = 5
currentCap = 2 
desCap = 5

the second row shows the prices per time slot. so 8 in total.

I appreciate any help thanks.

share|improve this question
1  
+1 well-structured question with all necessary info. –  Jim Garrison Mar 11 '11 at 18:04

4 Answers

up vote 1 down vote accepted

You are creating your array with maxCap and timeSlot while they still have the default value of 0. readInput() has not been called yet so how could you know what size to make the array?

Create the array after you read in maxCap and timeSlot.

share|improve this answer

When you create an object of the class Versie3, maxCap and timeSlot take their default value of 0 and the array opt is created with the size of 1 x 1.

After this you go and read the file which rewrites the values of maxCap and timeSlot but the array size remains unchanged.

To fix this, allocate memory for the array in function readFile after you've read the dimensions.

share|improve this answer

opt is getting intialized at construction time before timeSlot and maxcap are set.

So you create an array

private double[][] opt = new double[0 + 1][0 + 1];

You have to create the array in the readInput method after the user has entered the values.

share|improve this answer

You init your opt array before you determine what maxCap equals.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.