Title: Strings and Methods Part 2
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Strings and Methods Part 2

Note that the item numbers on this web page differ from what you see in the video, because the original web page was split into 2 web pages.

1. So why is there a String something or the other in main. If you run this from the command line, you will see that you can have parameters on the command line. These parameters get passed in through this String object named args ... we will discuss this in more detail later.
public static void main(String[] args)
2. The String class has many other methods. We will be doing more with these methods later in the semester. For fun, we will demo the first few. Some of the methods are:
  - String toLowerCase();
  - String toUpperCase();
  - String trim();
  - length();
  - char charAt(int position);
  - String substring(int start_position);
  - String substring(int start_position, int end_position);
  - int indexOf(char c);
  - int indexOf(char c, int start_position);
  - int indexOf(String toFind);
  - int indexOf(String toFind, int start_position);
3. Strings can also contain Funny Escape characters to allow imbedding a quote, apostrophe, \, new line, carriage return or tab character ... We can demo these if you like, however usage of some of these can violate the portability of your code. On Unix, PCs, and Macs for instance there are different characters for the end of a line.:
\"
\'
\\
\n
\r
\t
4. Most classes will also have methods. Consider the methods in the Scanner class:

String nextLine(); // returns next line of text input
String next(); // returns next input token (could be an int, double, String, etc.) as a String
double nextDouble(); // returns next double.
int nextInt(); // returns next int

Program created on the video



public class StringMethods {


    public static void main(String[] args) {
        String s = "  Elementary Elements   ";

        String s2 = s.toLowerCase();  //  elementary elements
        System.out.println("s2 ="+s2);

        s = s.toUpperCase();
        System.out.println("s="+s);

        s2 = s2.trim();
        System.out.println("s2="+s2);

        int len = s2.length();
        System.out.println("len = "+len);

        for (int i=0; i < len ;  i++)
        {
            char c = s2.charAt(i);
            System.out.println(" char at "+ i + " ="+ c);
        }

        int count =0;
        for (int i=0; i < len ;  i++)
        {
            char c = s2.charAt(i);
            if (c == 'e')
                count += 1;
        }

        System.out.println("found " + count + " e's");

        String s3 = s2.substring(6);
        String s4 = s2.substring(6,10);

        System.out.println("s2.substring(6)="+ s3);
        System.out.println("s2.substring(6,10)="+ s4);

        int index = s2.indexOf('e');
        System.out.println("index ="+index);

        index = s2.indexOf('e' , index +1);
        System.out.println("index ="+index);


        index = s2.indexOf('e' , index +1);
        System.out.println("index ="+index);


        index = s2.indexOf('e' , index +1);
        System.out.println("index ="+index);


        int start =0;
        int index2;
        int count2 =0;

        do
        {
            index2 = s2.indexOf('e', start);
            if ( index2 >= 0)
            {
                start = index2 +1;
                System.out.println("Found an e at " + index2);
                count2 += 1;
            }

        } while (index2 >= 0);

        System.out.println("Found " + count2 +" e's");

        start = 0;
        count2 = 0;
        do
        {
            index2 = s2.indexOf("men", start);
            if ( index2 >= 0)
            {
                start = index2 +1;
                System.out.println("Found men  at " + index2);
                count2 += 1;
            }

        } while (index2 >= 0);

        System.out.println("Found " + count2 +" men's");



    }

}




EscapeCharacters



public class EscapeCharacters {


    public static void main(String[] args) {

        String quote = "Lincoln said   \"Four score and seven years ....\"  ";
        String tabStuff = "team\tscore \tstandings";
        String s= "Detroit\t30\t5'th";
        String s2 = "first Line \nSecond Line\nThirdLine";

        System.out.println("quote="+quote);
        System.out.println(tabStuff);
        System.out.println(s);

        System.out.println(s2);

    }

}



Java Practice(JPHJ):

Create a program that will manipulate strings with the following steps:

1. Define a String named myStr and initialize it with the value
"Fred Flintstone follows the big T-Rex 2 miles to 1234 Rock Avenue."
For the String myStr do the following things:
  1. (1 point) Using a method from the String class, find the length of "myStr" and print it out.
  2. (1 point) Create a "for" loop to print out the output:


  F is the character at index 0
  r is the character at index 1
  e is the character at index 2
......
  u is the character at index 63
  e is the character at index 64
  . is the character at index 65


  3. (1 point) Create a "for" loop to find the number of   'f'   characters.
  4. (1 point) Use a method in the String class to replace myStr with a new value that has all lower case letters.
  5. (1 point) Find the index for the first character '2' and print it out.
  6. (1 point) Add 1 to the above index and find index of the next character '2' showing up in the myStr String. Print this index out.
  7. (2 points) Print out the value of the substring for myStr that starts with index 5 and ends with index 15.
  8. (2 points) What string do we get if we call the version of substring for myStr that takes just one parameter and pass it the value of 5. Print out the resultant value.

Last Modified: December 22, 2014 9:41 PM