564 Strings and Characters Chapter 10 (Photography web hosting) 19 char

564 Strings and Characters Chapter 10 19 char charArray[] = new char[ buffer.length() ]; 20 buffer.getChars( 0, buffer.length(), charArray, 0 ); 21 output += “nnThe characters are: “; 22 23 for ( int count = 0; count < charArray.length; ++count ) 24 output += charArray[ count ]; 25 26 buffer.setCharAt( 0, ‘H’ ); 27 buffer.setCharAt( 6, ‘T’ ); 28 output += “nnbuf = ” + buffer.toString(); 29 30 buffer.reverse(); 31 output += “nnbuf = ” + buffer.toString(); 32 33 JOptionPane.showMessageDialog( null, output, 34 “Demonstrating StringBuffer Character Methods”, 35 JOptionPane.INFORMATION_MESSAGE ); 36 37 System.exit( 0 ); 38 } 39 40 } // end class StringBufferChars Fig. 10.14 StringBufferclass character manipulation methods. 10.17 StringBufferappendMethods Class StringBuffer provides 10 overloaded append methods to allow various data- type values to be added to the end of a StringBuffer. Versions are provided for each of the primitive data types and for character arrays, Strings and Objects. (Remember that method toStringproduces a Stringrepresentation of any Object.) Each of the methods takes its argument, converts it to a String and appends it to the String- Buffer. The append methods are demonstrated in Fig. 10.15. [Note: Line 20 specifies the literal value 2.5f as the initial value of a float variable. Normally, Java treats a floating-point literal value as type double. Appending the letter fto the literal 2.5indicates to the compiler that 2.5should be treated as type float. Without this indication the compiler generates a syntax error, because a doublevalue cannot be assigned directly to a floatvariable in Java.] Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply