Owl Sams Teach Yourself Java 2 in 24 Hours

Hour 24: Making Your Knowledge Add Up

NOTES AND CORRECTIONS

SOURCE FILES

READER QUESTIONS

  • Question: In the MiniCalc solution, I am a little confused with how part of the program works. More specifically, I would like to know more about the following lines:
    • if (newText.indexOf(".") == -1)
    •     newText += ".";
  • Answer: The indexOf() method of the String class searches for text inside the string -- such as a "." character in the above example. If the text is found, indexOf() returns an integer indicating the position where it was found. If it is not found, indexOf() returns -1. The above lines make sure that there isn't a "." character in newText before adding one to the string. You can find out more from Sun's official documentation for the java.lang.String class.

ACTIVITIES