Java interview one question per day 2

Source: Internet
Author: User

Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.

Note: If you want to output the number of Chinese and the number of Chinese symbols separately, simply modify the IF statement in the Ischinese ()

Knowledge Supplement:

Java does not have a powerful function like scanf in C, mostly by defining an input-output stream object. The commonly used classes have bufferedreader,scanner.

1, using Scanner to read from the keyboard integer or float type data

Scanner as a supporter of system.in object, system.in to get the user input content, to scanner to do some processing.
Several methods are available in the Scanner class:
Next (): Gets a string;
Nextint (): Converts the obtained string to an integer of type int;
Nextfloat (): Converts the obtained string into float type;
Nextboolean (): Converts the obtained string to a Boolean type; it is very convenient to get the user's input with scanner, but the scanner gets the input based on the space character, including the SPACEBAR, tab, and enter. When you press any of these keys, Scanner will return the next input. When you include spaces in the middle of your input, it is clear that using scanner will not fully get the string you entered.

2, using BufferedReader to read the string from the keyboard and write into the file Abc.txt

The ReadLine () method returns all character input before the user presses the ENTER key, not including the last pressed enter return character

The ReadLine () method that uses the BufferedReader object must handle the java.io.IOException exception (Exception).

3. str.getbytes (); If CharSet is not written in parentheses, the Sytem.getproperty ("file.encoding") is used, which is how the current file is encoded,

Many people write the system's default encoding, which is not the case with code testing, and what is actually obtained is how the file is encoded.

Str.getbytes ("CharSet");//Specify CharSet to parse the underlying stored Unicode code into a byte array in charset encoded format

string New_str=new string (str.getbytes ("Utf-8"), "GBK"));

The parsed byte data into a GBK encoded string, in memory is the GBK format of the byte array into Unicode to pass the interaction, there will be garbled, and vice versa.

4.

Convert string to bytes, various encoding conversion to bytes different, such as UTF-8 each Chinese character to 3bytes, and GBK to 2bytes, so to explain the encoding method, otherwise with the default encoding.

is to convert a string of type strings to byte and into a byte array. All the data in Java is in bytes, and byte data can be stored in a byte array.

When storing character data (the string is character data), the table is checked first, then the results of the query are written to the device, and the reading is done by checking the table and printing the contents to the display device.
GetBytes () is converted using the default character set, GetBytes ("Utf-8") is converted using the UTF-8 encoded table.

5. In the course of testing, occasionally encountering the following threading problems

ERROR:JDWP unable to get JNI 1.2 environment, JVM->GETENV () Return code = 2
JDWP Exit Error Agent_error_no_jni_env (183): [http://www.cnblogs.com/../src/share/back/util.c:820]

Workaround:

At the end of the program, add the main () function:

System.exit (0);

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.io.UnsupportedEncodingException; Public classTesttwo { Public Static voidMain (String args[])throwsunsupportedencodingexception{intEn=0,num=0,space=0,others=0; BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); byte[] bytes=NULL; String a=""; System.out.println ("Please enter a string:"); Try{a=Br.readline (); }Catch(IOException e) {e.printstacktrace (); } bytes=a.getbytes ();  for(inti=0;i<bytes.length;i++){            CharB= (Char) bytes[i]; if(b>= ' A ' &&b<= ' Z ' | | b>= ' A ' &&b<= ' Z ') {en++; }Else if(b>= ' 0 ' &&b<= ' 9 ') {num++; }Else if(b== ' | | b== ") {Space++; }Else if(Ischinese (b)) {Others++; }            //String str = string.valueof (b);String C =NewString (Bytes, "UTF-8"); System.out.println (i+c); }        if(others>=3) {Others=others/3; } System.out.println ("En is:" +en); System.out.println ("Num is:" +num); System.out.println ("Space is:" +space); System.out.println ("Others is:" +others); System.exit (0); }     //Perfect for judging Chinese characters and symbols based on Unicode encoding    Private Static BooleanIschinese (Charc) {Character.unicodeblock UB=Character.UnicodeBlock.of (c); if(UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS | | ub = =Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS|| UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | | UB = =Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B|| UB = = Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION | | UB = =Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS|| UB = =Character.UnicodeBlock.GENERAL_PUNCTUATION) {            return true; }        return false; }}

Java interview one question per day 2

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.