Java Review

The Java API

The Object class methods

•      Boolean equals (Object obj)

–   Makes shallow comparison to determine equality

•      String toString( )

–   The method toString is implicitly invoked when an object is an argument of println

 

String classes

•      String class methods

–    st.length( ) returns an int

–    st.compareTo(str2) returns an int

–    st.substring(index1, index2) returns a String

–    st.trim( ) returns a String with the leading and trailing blanks removed

–    These strings are nonmutable

•      StringTokenizer

–    Constructors

•    One argument-- string to be tokenized

•    Two arguments– string plus the delimiters (space is the default)

–    strT.nextToken( ) returns the next token

–    strT.hasMoreTokens( ) returns a boolean

 

 Standard Java I/O

•      Standard output to the screen

–   System.out.println(“some string”)

–    println differs from print in that it terminates a line of output

•      Standard input from the keyboard

–    You need an object of type BufferedReader

–    Example:

BufferedReader myInput;
myInput = new BufferedReader(new
                         InputStreamReader(System.in);

String str = myInput.readLine( );

 

Reading integers

•      First create an object of type BufferedReader as in the previous slide to read the int first as a String

BufferedReader br = new BufferedReader(new
                         InputStreamReader(System.in);

•      Then convert it to an int

•      int i = Integer.parseInt(br.readLine( ));

•      parseInt is a static method of the predefined wrapper class Integer