Tip:
Highlight text to annotate it
X
Let’s take a look and see how the program runs. You can see here
that I set a breakpoint where I want it to stop.
I go up here and click on run (the green triangle icon).
And the program will stop right here.
You can also see that I’ve opened up my Locals window (at the bottom of the screen) so I can see all my
local variables
To do this go into Debug/Windows/Locals
I keep track of
my different variables.
I’ll step through this and now I can see
that the variable rawISBN is
the data from the textbook.
I’m going to look for the first dash
and that is going to go into the variable, extraCharIndex. I can either mouse over this and see
that the dash is index 5 or else I can look down here (in the Locals panel)
and see extraCharIndex is 5, down below.
So far, so good. I’ll go through there. Now I’m going take the
currentIndex (which is zero)
and the extraCharIndex, where we found the dash, and subtract it from the
currentIndex. You’ll see why I did this in a minute.
That will give us the starting point
and how many characters, in this example we are going to have 5 characters.
We are going to pull a substring( ) out and save that in the variable, cleanISBN.
And that’s right down here in the Locals panel. So, when I step through this line I should expect
to see 97801.
I should see this part right in here. I’ll step
through there and there is the cleanISBN number.
Now we want to move past
that first section. We are now pointing here, we want to move past there
and we are going to move our currentIndex to the 6th position.
Our extraCharIndex was 5...
and we found the dash, so we are going to move 1 past it, so that is going to be 6
which will be our currentIndex.
From that currentIndex we are going to start from here and look to see if there are any
more dashes.
We can see that there is one at position (index) 10.
Right here. So, it is greater then 1
so now we are going to take the currentIndex, which is 6,
and we are going to subtract 6 from 10 and that
is going to give us 4 characters. We are going to put that
add that into the variable cleanISBN.
If we look at our cleanISBN we should see 97801 1234.
Let’s give it a try.
What happened? We see we had 1234
so it looks like we have a problem.
It does show you a good debugging technique though: Whenever you do a line
you think and enunciate “What is going to happen next?”
So, we expected to see 97801234.
Instead all we got was the 1234.
If you look closely at this, you will see that
we are assigning that block “1234"
to our cleanISBN. What we want to do is append it.
So, we need to change this ampersand “&”
to an ampersand plus “&+” so it appends the information to our cleanISBN variable instead of overwriting it.
So, I’ll stop the program. Now, I know I’m pretty good
all the way up to here, so I’m going to move my breakpoint here
to speed up my development a little bit
and run the program again.
And, there the program stops and I can see I
have 97801, that first step,
and I’ll step into it.
The currentIndex is 6.
My dash is at 10. I should get 4 characters
and the program should add it to the end of the variable. Let’s see if it does that.
And there, that is exactly what we want.
With the dashes stripped off and we have all our numbers.