/**********************************************************************
 * Written by Ben Mathew
 * for Cornell University CS 100 Summer 2001
 * 
 * This program examines the String.length() and String.charAt() methods.
 * In addition, it covers usage of the multiple String.indexOf() methods. 
 * All of these methods are very important and useful in the analysis and
 * and manipulation of Strings.
 **********************************************************************/

public class StringIndexMethods
{
    public static void main(String args[])
    {
        
        //some sample strings to work with
        String op1 = "Once upon a time";
        String op2 = "A long time ago";
        String op3 = "The other day";
        //            0123456789012345
        //if you use a fixed width font, then the numbers in the comment will
        //line up with the correct index of each character in the String
			   
        //Start by printing out the lengths of the Strings
        System.out.println(op1.length());
        System.out.println(op2.length());
        System.out.println(op3.length());
        //Note that the length of the String is calculated by the total number
        //of characters in the String INCLDUING WHITESPACE!!! Even terminal
        //whitespace would be counted in the String's length. Also, note that
        //you must use the () to get this to work!
  
      
        //two in one 1 operator precedence and the charAt operator

        //first, we will look at the String.charAt() operator. This method works
        //by typing .charAt(int) after the name of a String variable in your
        //program, replacing the int with a valid integer value. By valid, this
        //is what is meant - A String has as many characters as it has length.
        //So since String op1 has a length of 16, it has 16 characters. But look
        //above, see how the first number lined up with the beginning of the
        //Strings is 0? This is the index of the first character in a String.
        //Instead of starting at 1, Java chooses to start the index at 0.
        //Therefore, printing out op1.charAt(0) would print out O. Make sure
        //you practice this and get used to it. The indexing will come up again
        //later in the course.

        //Now, the code below should print out a message. See if you can tell
        //what it should say, without running the program first. :)
        System.out.println();
        System.out.println(op1.charAt(1) + op1.charAt(15) + op1.charAt(10) +
                           op2.charAt(7) + op2.charAt(6) + op1.charAt(2) +
                           op3.charAt(4) + op3.charAt(10) + op3.charAt(7));
        //Okay, now run the program and see if you were correct. What's that?
        //You got a line of numbers? Well, that's what my comment above about
        //the two in one was concerning. Unlike Strings, characters will not
        //concatenate themselves together. Instead, using the + operator on
        //two characters will add together their ASCII values! So, how do we
        //fix this? Add the code "" + in between the ( and op1.charAt(1) in
        //the lines of code above. This will cause Java to realize you want
        //to make a String and it will concatenate all of the characters to
        //the String to give you the message :)


	//Now, the last point, is indexOf and lastIndexOf. These commands will
        //allow you to search through Strings.
        
        int place; //place holder variable

        //String.indexOf(String) is applied like String.charAt(int) but instead
        //of an integer, you use a String value. So let's search the first
        //String (op1) for the letter "u"

        place = op1.indexOf("u");
        System.out.println();
        System.out.println("u was found at index: " + place);
        System.out.println();
        //If we look back at the String for op1, we will see that it is the
        //sixth character. But recall that Java indices start with 0, so it
        //should return that u is in the fifth index, and sure enough, it does!

        //what about "e", that appears two times in the String op1
        place = op1.indexOf("e");
        System.out.println("e was found at index: " + place);
        System.out.println();
        //why does it only print out one number? indexOf will only find the
        //first occurrence of the String you tell it to search for. But, there
        //is a way around this. If you use String.indexOf(String, int) then you
        //can tell Java how far in the String you want to search. Now that we
        //know where the first place "e" is, it's stored in the variable place,
        //so lets try again, starting at the next index.
        place = op1.indexOf("e", (place+1));
        System.out.println("Another e was found at index: " + place);
        System.out.println();
        //There we go, it found the second e this time :)
        //String.lastIndexOf() is the same as String.indexOf(), but instead it
        //will start searching from the last character, instead of the character
        //at index 0;

        //The last example is looking for whole words. Rememver, indexOf can take
        //Strings, not just single letters.
        place = op1.indexOf("upon");
        System.out.println("The String upon starts at index: " + place);
        System.out.println();

        //Oh, and one more thing. What if Java doesn't find the String you're
        //looking for? Well, run the program and see :)
        place = op3.indexOf("upon");
        System.out.println("The String upon starts at index: " + place);
        System.out.println();

        //-1? That's not a valid index - This is like what Savitch calls a
        //sentinel value - since -1 is not possible for an index, you can
        //check if the index you get while searching is negative. If it is,
        //then you know that what you were searching for wasn't found.
        //These examples are pretty simple, but there are a lot of comments,
        //and there are many more things for you to try out. Make sure you
        //spend some time writing your own examples. It's impossible to get
        //good at programming without writing your own code.
        //
        //- b
        
    }

}
