[Question 1] compileProgramInput a character in the terminal and output its ASCII code
1:Public ClassToasc
2:{
3:Public Static VoidMain (string [] ARGs)
4:{
5:Required SC =NewUsing (system.In);
6:String S = SC. nextline (); // The scanner scans the input data.
7:For(IntI = 0; I <S. Length (); I ++)
8:{
9:CharC = S. charat (I); // extract characters from the string
10:IntA = (Int) C; // convert characters into ASCII codes
11:System.Out. Println (C +"The ASCII code is"+ );
12:
13:}
14:}
15:
16:}
Test data:
To be or
The ASCII code of T is 116.
O's ASCII code is 111
The ASCII code of is 32
B's ASCII code is 98
The ASCII code of E is 101.
The ASCII code of is 32
O's ASCII code is 111
The r ASCII code is 114.
【Question 2]Write a program to search for strings with the required format, and output the repeated strings andString.
1:Public ClassPipei
2:{
3:
4:Public Static VoidMain (string [] ARGs)
5:{
6:Pattern P = pattern. Compile ("Java .*");// Declare the string to be matched
7:Matcher M = P. matcher ("Java is not a person");// Match the string to be matched
8:Boolean B = M. Matches ();
9:If(B)
10:{
11:System.Out. Print ("Matched correctly");
12:
13:}
14:ElseSystem.Out. Print ("Matching error");
15:Required SC =NewUsing (system.In);
16:String S1 = SC. nextline ();
17:Hashset hash1 =NewHashset ();
18:Hashset hash2 =NewHashset ();
19:String s [] = s1.split ("");
20:For(IntI = 0; I <S. length; I ++)
21:{
22:If(! Hash1.add (s [I])// Do not add the same string
23:Hash2.add (s [I]);// Add the same string not added by hash1
24:
25:}
26:Hash1.removeall (hash2 );// Set the hash1-hash2 to get strings with different occurrences
27:System.Out. Println ("Different words :"+ Hash1 );
28:System.Out. Println ("The same words are :"+ Hash2 );
29:
30:}
31:}
Test:
Matched correctly
To be or not to be
Different words: [not, or]
the same words are: [to, be]