Archive for November, 2007

572 Strings and Characters Chapter 10 equivalent. The (Cheapest web hosting)

Friday, November 23rd, 2007

572 Strings and Characters Chapter 10 equivalent. The method returns the converted character if the character has a lowercase equivalent; otherwise, the method returns its original argument. Figure 10.18 demonstrates static Character methods digit and forDigit, which perform conversions between characters and digits in different number systems. Common number systems include decimal (base 10), octal (base 8) , hexadecimal (base 16) and binary (base 2). The base of a number is also known as its radix. For more information on conversions between number systems, see Appendix E. Line 52 uses method forDigitto convert the integer digitinto a character in the number system specified by the integer radix(also known as the base of the number). For example, the integer 13in base 16 (the radix) has the character value ‘d’. Note that the lowercase and uppercase letters are equivalent in number systems. Line 77 uses method digit to convert the character c into an integer in the number system specified by the integer radix(i.e., the base of the number). For example, the character ‘A’in base 16 (the radix) has the integer value 10. 1 // Fig. 10.18: StaticCharMethods2.java 2 // Demonstrates the static character conversion methods 3 // of class Character from the java.lang package. 4 5 // Java core packages 6 import java.awt.*; 7 import java.awt.event.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class StaticCharMethods2 extends JFrame { 13 private char c; 14 private int digit, radix; 15 private JLabel prompt1, prompt2; 16 private JTextField input, radixField; 17 private JButton toChar, toInt; 18 19 public StaticCharMethods2() 20 { 21 super( “Character Conversion Methods” ); 22 23 // set up GUI and event handling 24 Container container = getContentPane(); 25 container.setLayout( new FlowLayout() ); 26 27 prompt1 = new JLabel( “Enter a digit or character ” ); 28 input = new JTextField( 5 ); 29 container.add( prompt1 ); 30 container.add( input ); 31 32 prompt2 = new JLabel( “Enter a radix ” ); 33 radixField = new JTextField( 5 ); 34 container.add( prompt2 ); 35 container.add( radixField ); Fig. 10.18 Characterclass staticconversion methods (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 571 Fig. 10.17

Thursday, November 22nd, 2007

Chapter 10 Strings and Characters 571 Fig. 10.17 staticcharacter testing methods and case conversion methods of class Character(part 3 of 3). Line 64 uses Character method isDigit to determine whether character c is a defined Unicode digit. If so, the method returns true; otherwise, it returns false. Line 66 uses Character method isJavaIdentifierStart to determine whether cis a character that can be used as the first character of an identifier in Java i.e., a letter, an underscore (_) or a dollar sign ($). If so, the method returns true; otherwise, it returns false. Line 68 uses method Character method isJavaIdentifier- Part to determine whether character c is a character that can be used in an identifier in Java i.e., a digit, a letter, an underscore (_) or a dollar sign ($). If so, the method returns true; otherwise, it returns false. Line 69 uses method Character method isLetter to determine whether character cis a letter. If so, the method returns true; otherwise, it returns false. Line 71 uses method Charactermethod isLetterOrDigitto determine whether character cis a letter or a digit. If so, the method returns true; otherwise, it returns false. Line 72 uses method Charactermethod isLowerCaseto determine whether character c is a lowercase letter. If so, the method returns true; otherwise, it returns false. Line 73 uses method Charactermethod isUpperCaseto determine whether character cis an uppercase letter. If so, the method returns true; otherwise, it returns false. Line 74 uses method Charactermethod toUpperCaseto convert the character c to its uppercase equivalent. The method returns the converted character if the character has an uppercase equivalent; otherwise, the method returns its original argument. Line 75 uses method Charactermethod toLowerCaseto convert the character cto its lowercase Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

570 Strings and Characters Chapter 10 49 50 (Msn web hosting)

Wednesday, November 21st, 2007

570 Strings and Characters Chapter 10 49 50 container.add( inputField ); 51 52 outputArea = new JTextArea( 10, 20 ); 53 container.add( outputArea ); 54 55 setSize( 300, 250 ); // set the window size 56 show(); // show the window 57 } 58 59 // display character info in outputArea 60 public void buildOutput() 61 { 62 outputArea.setText( 63 “is defined: ” + Character.isDefined( c ) + 64 “nis digit: ” + Character.isDigit( c ) + 65 “nis Java letter: ” + 66 Character.isJavaIdentifierStart( c ) + 67 “nis Java letter or digit: ” + 68 Character.isJavaIdentifierPart( c ) + 69 “nis letter: ” + Character.isLetter( c ) + 70 “nis letter or digit: ” + 71 Character.isLetterOrDigit( c ) + 72 “nis lower case: ” + Character.isLowerCase( c ) + 73 “nis upper case: ” + Character.isUpperCase( c ) + 74 “nto upper case: ” + Character.toUpperCase( c ) + 75 “nto lower case: ” + Character.toLowerCase( c ) ); 76 } 77 78 // execute application 79 public static void main( String args[] ) 80 { 81 StaticCharMethods application = new StaticCharMethods(); 82 83 application.addWindowListener( 84 85 // anonymous inner class 86 new WindowAdapter() { 87 88 // handle event when user closes window 89 public void windowClosing( WindowEvent windowEvent ) 90 { 91 System.exit( 0 ); 92 } 93 94 } // end anonymous inner class 95 96 ); // end call to addWindowListener 97 98 } // end method main 99 100 } // end class StaticCharMethods Fig. 10.17 staticcharacter testing methods and case conversion methods of class Character(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting ratings - Chapter 10 Strings and Characters 569 Line 63

Wednesday, November 21st, 2007

Chapter 10 Strings and Characters 569 Line 63 uses Charactermethod isDefinedto determine if character cis defined in the Unicode character set. If so, the method returns true; otherwise, it returns false. 1 // Fig. 10.17: StaticCharMethods.java 2 // Demonstrates the static character testing methods 3 // and case conversion methods of class Character 4 // from the java.lang package. 5 6 // Java core packages 7 import java.awt.*; 8 import java.awt.event.*; 9 10 // Java extension packages 11 import javax.swing.*; 12 13 public class StaticCharMethods extends JFrame { 14 private char c; 15 private JLabel promptLabel; 16 private JTextField inputField; 17 private JTextArea outputArea; 18 19 // set up GUI 20 public StaticCharMethods() 21 { 22 super( “Static Character Methods” ); 23 24 Container container = getContentPane(); 25 container.setLayout( new FlowLayout() ); 26 27 promptLabel = 28 new JLabel( “Enter a character and press Enter” ); 29 container.add( promptLabel ); 30 31 inputField = new JTextField( 5 ); 32 33 inputField.addActionListener( 34 35 // anonymous inner class 36 new ActionListener() { 37 38 // handle text field event 39 public void actionPerformed( ActionEvent event ) 40 { 41 String s = event.getActionCommand(); 42 c = s.charAt( 0 ); 43 buildOutput(); 44 } 45 46 } // end anonymous inner class 47 48 ); // end call to addActionListener Fig. 10.17 staticcharacter testing methods and case conversion methods of class Character(part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

568 Strings and Characters Chapter 10 45 buffer.deleteCharAt( (Virtual web hosting)

Tuesday, November 20th, 2007

568 Strings and Characters Chapter 10 45 buffer.deleteCharAt( 10 ); // delete 5 in 2.5 46 buffer.delete( 2, 6 ); // delete .333 in 33.333 47 48 output += 49 “nnbuffer after deletes:n” + buffer.toString(); 50 51 JOptionPane.showMessageDialog( null, output, 52 “Demonstrating StringBufferer Inserts and Deletes”, 53 JOptionPane.INFORMATION_MESSAGE ); 54 55 System.exit( 0 ); 56 } 57 58 } // end class StringBufferInsert Fig. 10.16 StringBufferclass insertand deletemethods (part 2 of 2). 10.19 CharacterClass Examples Java provides a number of classes that enable primitive variables to be treated as objects. The classes are Boolean, Character, Double, Float, Byte, Short, Integer and Long. These classes (except Booleanand Character) are derived from Number. These eight classes are known as type wrappers, and they are part of the java.lang package. Objects of these classes can be used anywhere in a program that an Objector a Number is expected. In this section, we present class Character the type-wrapper class for characters. Most Characterclass methods are static, take at least a character argument and perform either a test or a manipulation of the character. This class also contains a constructor that receives a charargument to initialize a Characterobject and several non- static methods. Most of the methods of class Character are presented in the next three examples. For more information on class Character(and all the wrapper classes), see the java.langpackage in the Java API documentation. Figure 10.17 demonstrates some static methods that test characters to determine whether they are a specific character type and the staticmethods that perform case conversions on characters. Each method is used in method buildOutput of class StaticCharMethods. You can enter any character and apply the preceding methods to the character. Note the use of inner classes for the event handling as demonstrated in Chapter 9. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 567 any position (Web site)

Monday, November 19th, 2007

Chapter 10 Strings and Characters 567 any position in a StringBuffer. Method delete takes two arguments the starting subscript and the subscript one past the end of the characters to delete. All characters beginning at the starting subscript up to, but not including the ending subscript are deleted. Method deleteCharAttakes one argument the subscript of the character to delete. Invalid subscripts cause both methods to throw a StringIndexOutOfBounds- Exception. The insertand delete methods are demonstrated in Fig. 10.16. 1 // Fig. 10.16: StringBufferInsert.java 2 // This program demonstrates the insert and delete 3 // methods of class StringBuffer. 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringBufferInsert { 9 10 // test StringBuffer insert methods 11 public static void main( String args[] ) 12 { 13 Object o = “hello”; 14 String s = “good bye”; 15 char charArray[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; 16 boolean b = true; 17 char c = ‘K’; 18 int i = 7; 19 long l = 10000000; 20 float f = 2.5f; 21 double d = 33.333; 22 StringBuffer buffer = new StringBuffer(); 23 24 buffer.insert( 0, o ); 25 buffer.insert( 0, ” ” ); 26 buffer.insert( 0, s ); 27 buffer.insert( 0, ” ” ); 28 buffer.insert( 0, charArray ); 29 buffer.insert( 0, ” ” ); 30 buffer.insert( 0, b ); 31 buffer.insert( 0, ” ” ); 32 buffer.insert( 0, c ); 33 buffer.insert( 0, ” ” ); 34 buffer.insert( 0, i ); 35 buffer.insert( 0, ” ” ); 36 buffer.insert( 0, l ); 37 buffer.insert( 0, ” ” ); 38 buffer.insert( 0, f ); 39 buffer.insert( 0, ” ” ); 40 buffer.insert( 0, d ); 41 42 String output = 43 “buffer after inserts:n” + buffer.toString(); 44 Fig. 10.16 StringBufferclass insertand deletemethods (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

566 Strings and Characters Chapter 10 Fig. 10.15 (Web hosting isp)

Monday, November 19th, 2007

566 Strings and Characters Chapter 10 Fig. 10.15 StringBufferclass appendmethods (part 2 of 2). Actually, StringBuffers and the append methods are used by the compiler to implement the +and +=operators for concatenating Strings. For example, assuming the following declarations: String string1 = “hello”; String string2 = “BC” int value = 22; the statement String s = string1 + string2 + value; concatenates “hello”, “BC”and 22. The concatenation is performed as follows: new StringBuffer().append( “hello” ).append( “BC” ).append( 22 ).toString(); First, Java creates a StringBuffer, then appends to the StringBufferthe String “hello”, the String”BC”and the integer 22. Next, StringBuffer s toString converts the StringBuffer to a String representation and the result is assigned to Strings. The statement s += “!”; is performed as follows: s = new StringBuffer().append( s ).append( “!” ).toString() First, Java creates a StringBuffer, then appends to the StringBuffer the current contents of s followed by “!”. Next, StringBuffer s toString converts the StringBufferto a Stringrepresentation and the result is assigned to s 10.18 StringBufferInsertion and Deletion Methods Class StringBufferprovides nine overloaded insertmethods to allow various data- type values to be inserted at any position in a StringBuffer. Versions are provided for each of the primitive data types and for character arrays, Strings and Objects. (Remember that method toString produces a String representation of any Object.) Each of the methods takes its second argument, converts it to a Stringand inserts it preceding the index specified by the first argument. The index specified by the first argument must be greater than or equal to 0and less than the length of the StringBuffer; otherwise, a StringIndexOutOfBoundsException is generated. Class String- Bufferalso provides methods delete and deleteCharAt for deleting characters at Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters (Business web hosting) 565 1 //

Sunday, November 18th, 2007

Chapter 10 Strings and Characters 565 1 // Fig. 10.15: StringBufferAppend.java 2 // This program demonstrates the append 3 // methods of the StringBuffer class. 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringBufferAppend { 9 10 // test StringBuffer append methods 11 public static void main( String args[] ) 12 { 13 Object o = “hello”; 14 String s = “good bye”; 15 char charArray[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; 16 boolean b = true; 17 char c = ‘Z’; 18 int i = 7; 19 long l = 10000000; 20 float f = 2.5f; 21 double d = 33.333; 22 StringBuffer buffer = new StringBuffer(); 23 24 buffer.append( o ); 25 buffer.append( ” ” ); 26 27 buffer.append( s ); 28 buffer.append( ” ” ); 29 buffer.append( charArray ); 30 buffer.append( ” ” ); 31 buffer.append( charArray, 0, 3 ); 32 buffer.append( ” ” ); 33 buffer.append( b ); 34 buffer.append( ” ” ); 35 buffer.append( c ); 36 buffer.append( ” ” ); 37 buffer.append( i ); 38 buffer.append( ” ” ); 39 buffer.append( l ); 40 buffer.append( ” ” ); 41 buffer.append( f ); 42 buffer.append( ” ” ); 43 buffer.append( d ); 44 45 JOptionPane.showMessageDialog( null, 46 “buffer = ” + buffer.toString(), 47 “Demonstrating StringBuffer append Methods”, 48 JOptionPane.INFORMATION_MESSAGE ); 49 50 System.exit( 0 ); 51 } 52 53 } // end StringBufferAppend Fig. 10.15 StringBufferclass appendmethods (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

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

Saturday, November 17th, 2007

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

Chapter 10 Strings and Characters 563 the characters

Saturday, November 17th, 2007

Chapter 10 Strings and Characters 563 the characters are truncated to the specified length (i.e., the characters in the String- Bufferafter the specified length are discarded). If the specified length is greater than the number of characters currently in the StringBuffer, null characters (characters with the numeric representation 0) are appended to the StringBufferuntil the total number of characters in the StringBuffer is equal to the specified length. 10.16 StringBufferMethods charAt, setCharAt, getCharsand reverse Class StringBufferprovides the charAt, setCharAt, getChars and reverse methods to manipulate the characters in a StringBuffer. Method charAttakes an integer argument and returns the character in the StringBuffer at that index. Method setCharAttakes an integer and a character argument and sets the character at the specified position to the character argument. The index specified in the charAt and set- CharAt methods must be greater than or equal to 0 and less than the StringBuffer length; otherwise, a StringIndexOutOfBoundsExceptionis generated. Common Programming Error 10.5 Attempting to access a character that is outside the bounds of a StringBuffer (i.e., an index less than 0 or an index greater than or equal to the StringBuffer s length) results in a StringIndexOutOfBoundsException. Method getCharsreturns a character array containing a copy of the characters in the StringBuffer. This method takes four arguments the starting index from which characters should be copied in the StringBuffer, the index one past the last character to be copied from the StringBuffer, the character array into which the characters are to be copied and the starting location in the character array where the first character should be placed. Method reverse reverses the contents of the StringBuffer. Each of these methods is demonstrated in Fig. 10.14. 1 // Fig. 10.14: StringBufferChars.java 2 // The charAt, setCharAt, getChars, and reverse methods 3 // of class StringBuffer. 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringBufferChars { 9 10 // test StringBuffer character methods 11 public static void main( String args[] ) 12 { 13 StringBuffer buffer = new StringBuffer( “hello there” ); 14 15 String output = “buffer = ” + buffer.toString() + 16 “nCharacter at 0: ” + buffer.charAt( 0 ) + 17 “nCharacter at 4: ” + buffer.charAt( 4 ); 18 Fig. 10.14 StringBufferclass character manipulation methods. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01