This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Yet ANOTHER string problem.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Yet ANOTHER string problem...." Watch "Yet ANOTHER string problem...." New topic

Yet ANOTHER string problem....

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
OK, OK, i admit that over the past 3 weeks or so, I have put up quite a few queries regarding strings, and nothing else.
I've neglected the javaranch assignments, etc..., just to read this colussus chapter on strings in Ivor Horton's epic "Beginning Java 2".
Well, I have yet another query regardiong strings.....
Below is a program that reads a string of text, and displays the number of occurences of the words 'and' and 'the'. I broadly follow it, but I have a query or two, regarding it.
The code for the program is listed below.

First off
Why is the variable index initialised to the value -1, as in :-
int index = -1;
What;s going on with the lines of code :-
while(index >= 0)
++ andCount;
index += andStr.length(); // Step to position after last 'and'
index = text.indexOf(andStr, index);
Ok, i know we're saying while the index is greater than 0, i.e., provided we find an occurence of 'and', increase the value of andCount. But what's going on, on the 2 lines after that?
I have spent an eternity redaing the chapter on arrays and strings in this text book (Ivor Horton's book). Simply because I've been told that string manipulation is fundamental to Java. Is this really the only way to learn - i.e., reading waffly, long-winded text books?

John Bonham was stronger, but Keith Moon was faster.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Initializing the index to -1 has a certain philosophical niceness to it.

A string has something at the zeroeth character - the 'first' character of the string; it's that whole 'off by one' thing.
So because zero is an actual valid index for a string and has "meaning", the author would choose -1. This initializes the variable and has the added benefit of meaning "I'm no where near the string yet" or imagine a pointer that is sitting at the imaginary position of 'just before the start of the string'.

I also checked the API to be sure there was no special meaning for negative numbers in the indexOf method: There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

As for the next two lines of code...

When you read a book you use a bookmark to mark your place. That is the job of the variable index. But not only does it mark where you last stopped reading, it indicates where you should start reading again.

To find the very first instance of the word, start at the beginning of the string (index = -1). When you find an instance of the word, place the bookmark there (reset index from -1 to the index of the start of the word).

index = text.indexOf(andStr, index);

Assuming this returns a positive index, we've found a word, so let's remember that we found the word...

++ andCount;

But now we are at the *beginning* of the word.. let's skip the word so we don't count it again. Adjust the value of our index to be just *after* the word.

index += andStr.length();

And now repeat the procedure, from our point just after the just-found word, again moving our 'bookmark' (adjust index from "just after the last found word" to "start of the next found word").

index = text.indexOf(andStr, index);

The counting and moving and searching are all in a loop until index is -1 (word not found).
[ February 02, 2003: Message edited by: Mike Curwen ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

As for this one: Is this really the only way to learn - i.e., reading waffly, long-winded text books?

The textbooks have plenty of sample code and the like, but also plenty of "blah, blah, blah" that seems really esoteric and not interesting.

I think the best way to learn is doing. It seems like silly or often-heard advice, but really the best way to learn is to write little 'nothing' programs; programs that start their life with your thought along the lines of "I wonder if I can do **this** with Java?"

If you suffer from a lack of imagination (like I do), you can at least use the textbooks for a starting point. For example.. if you modified the above program, and put in a whole bunch of System.out.println("index is now: " + index) , I bet you would have figured it out.

And of course, the CattleDrive.
Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
Seems a bit heavy - i'll need a bit of time to make it sink in, so that i understand it, but i think i can follow the explanations.
Thanks for the help.
I agree. Here's the link:
subject: Yet ANOTHER string problem....
Similar Threads
To retrive a particular line from the file
June Newsletter Puzzle
How to do a fast substring search on an Array of strings?
JText Area..searching and replacing text
pass values to static method