(c) Copyright 2000, 2001, 2002, 2003, 2004  by Daniel Berleant

 

Announcements

 

See Web for updated HW2

Any general questions about the course?

    ...you may ask now, during break, or after class

    (reminder: 3 weeks of Java then 13 weeks of UP)

    Want to print out notes in small font to save ink?

        Bring up URL in browser

        Click "File," click "Edit with,"

        Select entire document with mouse

        Change font to 10 pt. (or whatever)

        Print

        Bring to class to write on, etc.

 

 

New  topic: Strings (and StringBuffers)

 

 

 


Converting Strings to Numbers


Converting Strings to numbers



More About Strings



 

String lengths



 

More Ways to Initialize Strings



str.charAt(3);


 
str.equals("cde");


 

String and StringBuffer

-------------------------------------------- 

Here's something a bit unexpected:

    Strings canNOT be modified!

String s=new String("hElLo");

System.out.println(s.toLowerCase( ));

System.out.println(s);

s is not modified!

Sometimes we want to modify a string

        Method 1:

            reassign the String "handle" (pointer):

            String s;

        s=myFile.read( );

        doSomethingWith(s);

                   s=myFile.read( );

        //re-assigned s

        Method 2: use the StringBuffer class...

 

Example StringBuffer constructors:

 

StringBuffer( ); 

//makes an empty buffer

//with 16 spaces in it

 

StringBuffer(50);

//makes a buffer with 50 spaces

 

StringBuffer ("abc");

//makes a buffer with 3+16 spaces

 

 

Sample code:

public class ReverseIt {
  public static void main 
        (String args[ ]){ 
     System.out.println
           (reverse(args[0]));
  } 
  static String reverse(String myString){ 
    int count; 
    int length=myString.length( ); 
    StringBuffer reversed; 
    reversed=new StringBuffer(length); 
    for (count=length-1;  
         count>=0; 
         count-- 
        ){                
      reversed.append 
      (myString.charAt(count)); 
    } 
       return reversed.toString( ); 
  } //end method  
} //end class 

 

class StringTokenizer

 

A string can have spaces (and punctuation, etc.)

A token is a piece of a string, like a word

...actually a word-like unit you an define

To get the tokens out,

    use a StringTokenizer object

import java.util.*;

      ...

      String myString=new

        String("Please tokenize" + 

               "this string.");

      StringTokenizer myTokenExtractor;

      myTokenExtractor=

         new StringTokenizer

             (myString );

      //myTokenExtractor=

      //new StringTokenizer

      //(myString, " \n\t\r,;.:?!")

        ////now punctuation also

      ////delimits

      //myTokenExtractor=

      //new StringTokenizer

      //(myString," \n\t\r,;.:?!",

      //    true);

      // //now delimiters are

      // //also tokens

      System.out.println

        (myTokenExtractor.

            countTokens( );

      while

         (myTokenExtractor.

             hasMoreTokens( ))

          System.out.println

             (myTokenExtractor.

               nextToken( ));

      

Let's look over the code to understand   

            What a string tokenizer is and how to construct it

            What methods it provides

            Q: Is a string tokenizer a String object

                 or another kind?

            Q: Is a string tokenizer associated with

                 a particular String?

            Q: Is a space a default delimiter?

            Q: Can any character be defined to be

                 a delimiter?

            Q: Does a string tokenizer remember where

                 it was in the String?