tags:

views:

145

answers:

7
/* Method: findName() */
/**
 * Finds the name of the entry and sets it to the name variable.
 */
    private void findName(String line){
        end = line.indexOf(" ");
        name = line.substring(0,end);
    }

/* Method: findDecades() */
/**
 * Finds the name of the entry and sets it to the name variable.
 */
    private void findDecades(String line){
        for(int i = 0; i<NDECADES; i++){
            int start = end + 1;
            end = line.indexOf(" ",start);
            String decadeTemp = line.substring(start, end);
            data[i] = Integer.parseInt(decadeTemp);
        }
    }

Line looks like the following:

Sam 58 69 99 131 168 236 278 380 467 408 466

When I run the code the first method runs fine but then it throws an error exception when it reaches the line:

            String decadeTemp = line.substring(start, end);

edit:

the error that is throw is:

throw new StringIndexOutOfBoundsException(endIndex - beginIndex);

+4  A: 

I suspect (without further info) that you're running off the end of your string during

line.substring(start, end);

and so your boundary conditions aren't right. I would print out the start/end values and determine from that what's wrong. I would strongly recommend, btw, that in questions like this you publish the exception/stacktrace you're getting.

Going further, you may find the above easier by using String.split(" ") and iterating across the resultant array. That tends to be less error prone since you can just use a for loop across the array, and not have to keep track of start and end pints (which I always get wrong, for what it's worth).

(as this is homework, I'm assuming the input strings are well-formed. Otherwise you'll have to cater for a variety of inputs and a more robust solution would be required)

Brian Agnew
There's already a way to have the computer show you relevant information when your program throws an unexpected exception: it's called a debugger. :-)
Ken
+4  A: 

Add another line

        end = line.indexOf(" ",start);
        System.out.println("Doing substring of "+start+" to "+end);  // new line
        String decadeTemp = line.substring(start, end);

That should make it obvious. I'm guessing that end is -1, which means you didn't find a space, and NDECADES is too big.

DaveC
Another thing to consider: Does every line end with a space? Should it end with a space or not? How will your program deal with a line that has the wrong number of integers in it or accidentally has multiple spaces in between them instead of just one. You might want to just use a Scanner instead, if that's allowed for the assignment.
MatrixFrog
+1 for recommending adding a debug line so they can see the mistake themselves instead of just having us tell him what it is.
MatrixFrog
+1  A: 

What if line doesn't have any spaces in it? Or what if it doesn't have any spaces in it from start on out?

Matt Greer
A: 

Problem solved NDECADES was one space too big.

Alex
A: 

To start finding the problem yourself you should have a look at the Javadoc for String#indexOf and String#substring, in particular about what happens in border cases (like nothing is found, etc).

Fabian Steeg
A: 

You need to do some checks, e.g.

if (start > 0 && end > 0 && end < length).

You also need to do check if decadeTemp is numeric.

fastcodejava
+1  A: 

You might want to take a look at some of the other methods on String that could make this a bit simpler (String.split() comes to mind).

John Meagher