Chpater 17. \ 'A' A ' ' '\n' Unicode \u 4 16 4 16 '\u4e00' char char ; = ; w kan kan char w ; char kan = ' '; Unicode System.out.println (int) Casting public class Kan { public static void main( String [ ] args ) { char kan=' ' ; System.out.println( " " + kan + " Unicode " + (int)kan ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-1
Unicode Unicode 28450 16 Integer.toHexString 16 Unicode System.out.println( " " + kan + " Unicode \\u" + Integer.toHexString( (int) kan ) ); Unicode u6f22 Java Shift JIS Unicode (char) Casting Unicode 0 9 '0' < '1' < '2' < '3' < '4' < '5' < '6' < '7' < '8' <'9' A Z Unicode M N L ( char ) ( 'M' + 1 ) 'N' ( char ) ( 'M' - 1 ) 'L' if char c, w; // c int digit ; if ( c == 'A' c == 'a' ) { // c Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-2
System.out.print( "A "); if ( 'A' <= c && c <= 'Z' ) { w = (char) (c + 32); // c // w 10 '0' '5' '5' - '0' 53-48 5 Unicode if ( '0' <= c && c <= '9' ) { digit = c - '0'; // c // 10 Java Unicode \u0020 \u8000 1616 public class GeneralScripts { public static void main( String [ ] args ) { for ( char code='\u0020' ; code < '\u8000' ; code++ ) { if ( code % 16 == 0 ) { System.out.print( "\n" + Integer.toHexString( (int) code ) ); System.out.print( " " + code ); Unicode \u4e00 \u4fff 1616 import java.awt.*; import java.applet.*; public class LocalScripts extends Applet { public void paint( Graphics gc ) { int base = 0x4e00; // for ( int code=base ; code < base + 0x1ff ; code++ ) { if ( code % 16 == 0 ) { gc.drawstring( Integer.toHexString( code ), 40, (code-base)/16 * 20 + 30 ); gc.drawstring( ""+(char)code, code%16 * 20 + 40, (code base)/16 * 20 + 30 ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-3
Mac OS X " \ Unicode 16 \u " \u4e00 \n" C Pascal Java String String String = String wow; String please = " "; wow = " " ; System.out.println( please ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-4
gc.drawstring( wow, 100, 20 ); String fickle = " " ; fickle = " " ; fickle = " " ; + String combined = " " + " " ; String complicated = " + ' ' + 30 + " " + 4.5e3 + " " ; System.out.println( combined + complicated ); extended String extended = " "; extended = extended + " "; extended += " "; // " " // " " == > String String length length( ).length( ) length 17 lengthofmaki 17 maki.length( ) System.out.println Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-5
length 6 String maki= " "; int lengthofmaki = maki.length( ); System.out.println( " maki " + lengthofmaki ); System.out.println( " " + " ".length( ) ); ==!= Identification Equivalence String equals( ) equalsignorecase( ) endswith( ) startswith( ) true false plane if String plane = "F16 Falcon"; if ( plane.equals( "F16 Falcon" ) ) { System.out.println( "Air Force Fighter" ); else if ( plane.equals( "F18 Hornet" ) ) { System.out.println( "Navy Fighter" ); else { System.out.println( "Is this a plane of US?" ); equals equalsignorecase if String mytext = "Igor Stranvinsky"; if ( mytext.equalsignorecase( "IGOR STRAVINSKY" ) ) { System.out.println( mytext + " composed the Fire Bird." ); if playing? When if false true String message = "When did you stop eating?"; if ( message.endswith( "playing?" ) ) // false { System.out.println( "Oh, I used to think I would like to study anytime." ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-6
if ( message.startswith( "When" ) ) // true { System.out.println( "I used to stop everything before noon" ); == < >= compareto compareto( ) 0 String source = "This is a small message."; int result1 = source.compareto( "This" ); // int result2 = source.compareto( "No, it's long." ); // int result3 = source.compareto( "This is a small message." ); // 0 int result4 = source.compareto( "This is not a small message." ); // int result5 = source.compareto( "What is the message?" ); // if 0 String target = "Rabbit" ; if ( target.compareto( "Lion" ) > 0 ) { System.out.println( "Lion proceeds the target" ); if ( target.compareto( "Turtle" ) < 0 ) { System.out.println( "Turtle follows the target" ); substring.substring(, ).substring( ) 0 length( ) -1 0 length( ) 3 97 String message = "Sample Message"; System.out.println( message.substring( 3, 10 ) ); System.out.println( message.substring( 7 ) ); // "ple Mes" // "Message" 5 5 substring length String firstfive = message.substring( 0, 5 ); // "Sampl" String lastfive = message.substring( message.length( ) - 5 ); // "ssage" Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-7
0-1 charat.charat( ) 0 Srting crazycats = " "; char firstchar = crazycats.charat( 0 ); // ' ' char lastchar = crazycats.charat( crazycats.length( ) - 1 ); // ' ' charat substring substring charat String katochan = " " ; String fourth = katochan.substring( 3, 4 ); // " " char fourthchar = katochan.charat( 3 ); // ' ' charat String takostr = " "; for ( int i =0; i < takostr.length( ); i++ ) { System.out.println( i + " " + takostr.charat( i ) + " " ); wordcount wordcount wordcount String message = "This is a sample message"; int wordcount = 1; for ( int i=0; i < message.length( ); i++ ) { if ( message.charat( i ) == 32 ) { wordcount++; // System.out.println( message + " " + wordcount ); if 32Unicode 32 if ( message.charat( i ) == ' ' ) { wordcount++; Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-8
String substring + String original = " " ; String modified = original.substring( 0, 2 ) + ' ' + original.substring( 3 ); // " " String String StringBuffer String StringBuffer StringBuffer String StringBuffer StringBuffer buffer = "A sample message"; StringBuffer String StringBuffer String StringBuffer setcharat.setcharat(, ); 0-1 StringBuffer StringBuffer String String StringBuffer takostr = " "; takostr.setcharat( 0, ' ' ); System.out.println( takostr ); // " " indexof( ) -1 indexof(, ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-9
String message = " "; int search = message.indexof( ' ' ); // 0 int search2 = message.indexof( ' ', search+1 ); // 4 replace(, ) String message = " "; String mymessage = message.replace( ' ', ' ' ); // " " StringBuffer replace(,, ) String sentence = ""; sentence.replace( 3, 6, " "); // "" tolowercase( ) touppercase( ) String message= "This is a simple message"; System.out.println( message.tolowercase( ) ); System.out.println( message.touppercase( ) ); // "this is a simple message" // "THIS IS A SIMPLE MESSAGE" trim( ) String whitespace = " Snow in the north field. "; System.out.println( whitespace.trim( ) ); // "Snow in the north field" Wrapper Class tostring 30 3.9 (new Integer(30)).toString( ) "30" (new Double(3.9)).toString( ) "3.9" (new Character( ' ' )).tostring( ) " " String valueof Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-10
String.valueOf( true ); // "true" String.valueOf( 12 ); // "12" String.valueOf( 12.34 ); // "12.34" String.valueOf( 'A' ); // "A" "" valueof tostring "" + 30 " 30" Integer Integer.parseInt( ) "39" 39 int thirtynine = Integer.parseInt( "39" ); Java2 Double Double.parseDouble( ) "-12.3e4" -12.3 104 double onetwothree = Double.parseDouble( "-12.3e4" ); Float.parseFloat Float.parseFloat( ) float fvalue = Float.parseFloat( "-12.3" ); JDK1.1 Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-11
int i = (new Integer( "45" )).intvalue( ); float f = (new Float( "34.5" )).floatvalue( ); double d = (new Double( "35.2e-3" )).doublevalue( ); "45" i "34.5"f "35.2e-3"d GUI Java Button TextField TextArea TextComponent new TextField( ) new TextArea(, ) settext gettext AWT 17-1 Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-12
init settext TextField tf = new TextField( 30 ) ; tf.settext( "Please input here." ); add( tf ); gettext( ) actionperformed String received = tf.gettext( ); tf.append( ": This is your input." ); paint import java.awt.*; import java.awt.event.*; import java.applet.*; public class TextInput extends Applet implements ActionListener { TextArea tarea; Button button; public void init( ) { tarea = new TextArea( 5, 40 ); button = new Button( "OK" ); button.addactionlistener( this ); add( tarea ); add( button ); public void actionperformed( ActionEvent e ) { String received = tarea.gettext( ); tarea.settext( " "+ received + " " ); repaint( ); 17-2 TextInput Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-13
TextField TextArea Return Enter actionperformed s, x, ch, shes y y ies s import import import java.awt.*; java.awt.event.*; java.applet.*; public class WordPlural extends Applet implements ActionListener { TextField singular, plural; public void init( ) { singular = new TextField( 20 ); plural = new TextField( 20 ); singular.addactionlistener( this ); add( singular ); add( plural ); public void actionperformed( ActionEvent e ) { String word = singular.gettext( ); if ( word.endswith( "s" ) word.endswith( "x" ) word.endswith( "ch" ) word.endswith( "sh" ) ) { plural.settext( word + "es" ); else if ( word.endswith( "y" ) ) { plural.settext( word.substring( 0, word.length( )-1 ) + "ies" ); else { plural.settext( word + "s" ); repaint( ); 17-3 WordPlural Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-14
String name = System.getProperty( "os.name" ); String lsep = System.getProperty( "line.separtor" ); Java Java tastream "39" 39 "23.45" 23.45 int w = Integer.parseInt( "39" ); double d = (Double.parseDouble("23.45"); TextArea TextField Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-15
TextField tf = new TextField( 10 ); // init add( tf ); // Applet String line = tf.gettext(); // actionperformed init actionperformed import java.awt.*; import java.awt.event.*; import java.applet.*; public class NumberOperation extends Applet implements ActionListener { TextField operand1, operand2; // TextArea result; // Button kick; // public void init( ) { operand1 = new TextField( 10 ); operand2 = new TextField( 10 ); result = new TextArea( 7, 20 ); kick = new Button( "Operation" ); kick.addactionlistener( this ); add( operand1 ); add( operand2 ); add( kick ); add( result ); public void actionperformed( ActionEvent e ) { int op1 = Integer.parseInt( operand1.gettext() ); int op2 = Integer.parseInt( operand2.gettext() ); String lsep = System.getProperty( "line.separtor" ); result.settext( "sum " + (op1 + op2) + lsep + "difference " + (op1 - op2) + lsep + "product " + (op1 * op2) + lsep + "quotient " + (op1 / op2) + lsep + "remainder " + (op1 % op2) + lsep + "greater " + ((op1 > op2)? op1 : op2) ); repaint( ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-16
17-4 NumberOperation java.text import java.text.*; NumberFormat nf = NumberFormat.getNumberInstance( ); nf.setmaximumfractiondigits( 3 ); nf.setminimumintegerdigits( 7 ); nf.setgroupingused( false ); String message = nf.format( 12.3458372 ); Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-17
Locale.CANADA Locale.CANADA_FRENCH Locale.CHINA Locale.CHINESE Locale.ENGLISH Locale.FRANCE Locale.FRENCH Locale.GERMAN Locale.GERMANY Locale.ITALIAN Locale.ITALY Locale.JAPAN Locale.JAPANESE Locale.KOREA Locale.KOREAN Locale.PRC Locale.SIMPLIFIED_CHINESE Locale.TAIWAN Locale.TRADITIONAL_CHINESE Locale.UK Locale.US DateFormat.DEFAULT DateFormat.FULL DateFormat.MEDIUM DateFormat.LONG DateFormat.SHORT import import java.text.*; java.util.*; DateFormat df = DateFormat.getDateInstance( DateFormat.DEFAULT, Locale.UK ); DateFormat tf = DateFormat.getTimeInstance( DateFormat.LONG, Locale.JAPAN ); String today = df.format( new Date( ) ); String now = tf.format( new Date( ) ); 17-1. TextArea 17-2. WordCounter if while wordcount 17-3. Nengo Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-18
1868 1912 1926 1989 Copyright by Tatsuo Minohara 2004 Rev. D Dec. 16th Macintosh Java Primer Chapter 17-19