tags:

views:

428

answers:

3

All right so I have agonized over this code for the past hour or so. This is the code for the class in question.

   public DistanceProject(String costs, String firstString,String secondString)
    {
        Scanner scan = new Scanner(costs);

        copyCost = scan.nextInt();
        replaceCost = scan.nextInt();
        deleteCost = scan.nextInt();
        insertCost = scan.nextInt();
        twiddleCost = scan.nextInt();
        killCost = scan.nextInt();
        stringFirst = firstString;
        stringSecond = secondString;

        maxFirst = stringFirst.length();
        maxSecond = stringSecond.length();

        costArray = new int[maxFirst+1][maxSecond+1];
        operations = new String[maxFirst+1][5];
        costArray2 = new int[maxFirst+1][maxSecond+1];
        operations2 = new String[maxFirst+1][maxSecond+1];
        System.out.println("This is MaxSecond: " + maxSecond);
    }



    private int copyCost;
    private int twiddleCost;
    private int replaceCost;
    private int insertCost;
    private int killCost;
    private int deleteCost;
    private String stringSecond;
    private int maxFirst;
    private int maxSecond;
    private String stringFirst;
    private int[][] costArray;
    private String[][] operations;
    private int[][] costArray2;
    private String[][] operations2;
    private int distanceMoves;
    private int finalCost;
    private int count;
    private static final int LARGE_NUMBER = 10000000;

}

But after running this code and calling the constructor, it seems that the 2nd dimension for all the arrays that are declared in the class. I was wondering if there was any particular reason as to why this array declaration is failing so hard. The print statement does actually output a valid number if given input.

If you want to run this yourself, the input is supposed to look like this:

6 ints a row (0 15 15 10 10 30) String (anything) String (String2)

My test input (that failed) was: 0 15 15 10 10 30 anything know

Here are the screenshots of what happens when I run it on my computer: Before:

After: Eclipse does a good job of highlighting what I mean in the second screenshot. The first picture shows that it has a value right before going on to declare the costArray without a second dimension.

A: 

As was mentioned in the comments it's difficult to tell exactly what your problem is here. One odd thing I noticed, however, is that you declare all of your variables outside of the class instead of inside it. This means that all instances of that class will share the same variables, instead of having their own copies. This could be causing behavior different from what your expecting.

TwentyMiles
All of the variables are instance variables, as long as they aren't declared static they're unique to individual objects of the DistanceProject class.
Mitch Flax
To add to what Mitch said: the variables are on the same level as the constructor, which would mean that they would be part of the class.
htw
You are correct. I mistakenly read the method definition as the class definition (which isn't actually present in the code).
TwentyMiles
A: 

I was wondering I was wondering if there was any particular reason as to why this array declaration is failing so hard.

I don't know either because I have ran the code and it runs as expected. No "failing" occurred.

Peter Lawrey
+1  A: 

Confirmed: it runs perfectly as expected. As long as there are six integers in the first parameter, it prints the length of the third parameter. Is this what you wanted?

Let me just confirm, what you intended was that:

if you call

DistanceProject("1 2 3 4 5 6", "abcd", "efg")

then

costArray = new int[maxFirst+1][maxSecond+1]

will give you an table of 20 integers (5 by 4, one for each pair of characters from the sets "abcd" and "efg", with a spare column and row)

Is this what you wanted? If not, please show us the actual code for the line that calls the method DistanceProject().

Sanjay Manohar