A program written about string handling during the summer vacation

Source: Internet
Author: User
Tags alphanumeric characters

write a function that intercepts a string, enter it as a string and a number of bytes, and the output is truncated by bytes the character string, but to ensure that the Chinese characters are not intercepted by half, such as "I abc", 4, should intercept "I AB", input "I abc Han DEF", 6, should output "I abc", instead of "I abc+ Han half".

Public static void main (string[] args) throws unsupportedencodingexception {
Scanner in=New Scanner (System. in);
System. out. println ("Input a String:");
String Str=in.next (); Task2_1


String c=cutstring(str,6);
System. out. println ("Output the String after cut:"+C);

}
private staticString Cutstring (String string,intLengththrowsunsupportedencodingexception {
/*lengththe number of bytes of string to intercept*/
    byte[] Byte=string.getbytes ("Unicode");
intn=0;/*Number of bytes*/
    inti=2;
for(; I<byte.length&&n<length;i++) {
if(i%2==1)
n++;
Else
if(byte[i]!=0)
n++;
}
if(i%2==1)
{
if(byte[i-1]!=0)
i--;
Else
           i++;
}
return newString (Byte,0,i,"Unicode");
}

Task2_2

If a string of words such as "AAAABBC China 1512" to separate the number of English characters, the number of Chinese characters, and the number of alphanumeric characters.

Public static voidMain (string[] args) {

Scanner in=NewScanner (System.inch);

System. out. println ("input a string including English,chinese,number and Others");
String Str=in.next ();

intchinumber=0;
intengnumber=0;
intnumber=0;
intothernumber=0;

for(intI=0;i<str.length (); i++)
{
CharGetchar=str.charat (i);
if(getchar>=' 0 '&&getchar<=' 9 ')
number++;
else if((getchar>=' A '&&getchar<=' Z ')|| (getchar>=' A '&&getchar<=' Z '))
engnumber++;
else if(Ischinese(GetChar))
chinumber++;
Else
            othernumber++;
}
System. out. println ("中文版 numbers is"+engnumber);
System. out. println ("Numbers is"+number);
System. out. println ("Chinese Numbers is:"+chinumber);
System. out. println ("Other Numbers is:"+othernumber);
}

private Static BooleanIschinese (CharGetChar) {
Character.unicodeblock Ub=character.unicodeblock. of(GetChar);
if(Ub==character.unicodeblock.cjk_compatibility_ideographs//CJKUnified ideographic Notation
            || Ub==character.unicodeblock.cjk_unified_ideographs//CJKcompatible with hieroglyphs
            || Ub==character.unicodeblock.cjk_unified_ideographs_extension_a//CJKUnified ideographic Symbol extensionA
            || Ub==character.unicodeblock.general_punctuation//Common Punctuation
            || Ub==character.unicodeblock.cjk_symbols_and_punctuation//Symbols and punctuation
            || Ub==character.unicodeblock.halfwidth_and_fullwidth_forms)//half-width and full-width form
        return True;
Else
return False;
}

1: To count the number of Chinese characters, you need to call the Ischinese method.

2: string in Java is not an array, more like a pointer, to get the characters in the first character, you need to call (Stringname). CharAt (i) .

 

Task2_3

create an interface, there is method one for the plot area, method two for the perimeter. Next create a graph class circle implementation with the above interface two methods for calculating the area and perimeter of the circle, and set a constant pi to denote pi, provide a non-parametric construction method to initialize the circle radius r is 1, and an argument to the construction method to initialize its radius r value, In the main method, create the object's top transformation object, find the circumference and area of the circle, and print the output.

Public static voidMain (string[] args) {
Object obj;
obj=NewCircle (4);
Circle c= (circle) obj;
System. out. println ("Circle 's area is"+ C.getarea (c));
System. out. println ("Circle ' s perimeter is"+c.getperimeter (c));
}
}
/*circleclass*/
classCircleImplementscount{
private DoubleR;
Private Final DoublePi= 3.14;
/*constructor Function*/
     PublicCircle () {
R= 1;
}
PublicCircleDoubleRADIUS) {
R=radius;
}
/*instantiate a method in an interface*/
    Public DoubleGetarea (Circle C) {
return(C.Pi) * (C.R) * (C.R);
}
Public DoubleGetperimeter (Circle C) {
return(C.Pi) * (C.R);
}

Interface

Public Interface Count {
double Getarea (Circle C);
double Getperimeter (Circle C);
}

1: idea ecilipase new come out, idea packge class in creating the class interface

2: On the Transition object, assuming b a b b reference assignment to a a a class object a is a subclass b object b

Eg:a A; or a A;

A=new B (); b b=new B ();

A=b;

Note: A newly created method and instance field in a subclass cannot be called on a transformed object

A program written about string handling during the summer vacation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.