Originally Posted by
xdrechsler
That clears it. I thought that the use of the indexOf("JAVA")!=-1 is to scan every 4- letter word from the start and when it does find the word "JAVA", there will be some values that is not equal to -1 that makes the if-statement satisfied and display the "J-A-V-A formed!".
Thank you for explaining it to me very well, I learned a lot from you.
Well, for informational purposes, using the last value of temp (JAVECJAVA), the method indexOf("JAVA") would return 5 I believe. Because that is the index of the start of the word JAVA in JAVECJAVA.
Indexes of Strings are one the most important thing to learn. It took me a while to figure out how string indexing worked so here is how I figured it out.
For this, think of the first row as the String and the second row and the indexes indicating the spaces between the letters (sorry about spacing, couldnt get the forum to post multiple spaces correctly).
. J A V E C J A V A
0 1 2 3 4 5 6 7 8 9
So if you called String.substring(3,6), which returns the string of the indexes between the selected ranges, you would get
ECJ. And if we called String.substring(5,9) (or String.substring(5) if you want from the index to the end) you would receive
JAVA.
Understanding the substring method and the indexing of strings is vital to understanding how to scan through a string or to cut off parts of strings.