• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding String indexOf() method  RSS feed

 
Karla Moreno
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm a beginner in Java and I need help understanding the String indexOf() method. For example, this was the code given to me by my instructor:


The output looks like this

Call me Ishmael.
The length of the string is 16
The first character is C
The last character is .
The first word is Call

I understand everything up until line 19 where I get confused at how indexOf() works.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karla Moreno wrote:
I understand everything up until line 19 where I get confused at how indexOf() works.


The indexOf() method returns the first index (location) of the character specified. In this example, the method returns the index of the space, which is then passed as the second parameter to the substring() method.

Henry

 
John Carlos
Greenhorn
Posts: 29
3
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding these to your program:

Note, the index is zero-based.  The first one starts at zero.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And note that indexOf() will return -1 if nothing is found, since zero is the index of the first character.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you follow the suggestion given earlier about printing indices, you will see it says space is at index 4; since indices are 0-based, that is the 5th keystroke. I think the behaviour of that method goes back to the old days of C; many functions which look for things return −1 to signify “not found”. You can use −1 because the users all know there is no such thing as index −1, and always returning the same value for “not found” allows you to program code to ignore anything returning −1. What else would you do for “not found”? Create an error state (which nobody will look at) or throw an Exception (which stops the program or at best slows execution)? No, return −1 which people are already familiar with.
So you have an index method which goes through the text letter by letter until it finds it is at the start of what you are looking for. Try changing the last line to finish ...s.indexOf("e I"))); and see what happens.

By the way: is that how you were told to indent code? Give 3 or 4 spaces per level of indentation, rather than 1 space, and don't write so many blank lines. I think it shou‍ld look like this (I am here using 3 spaces per level.):-
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!