Perform simple character and digit statistics on strings to explore the List function in java, javalist

Source: Internet
Author: User

Perform simple character and digit statistics on strings to explore the List function in java, javalist

Question:

Count the numbers and strings in a string and arrange them separately.

1. Numbers. Strings can be obtained from the keyboard.

2. Stored in list

3. count the number of digits and the number of strings

4. Output numbers and strings in ascending order

5. arrays cannot be used.

List usage

List includes all the implementation classes of the List interface and List interface. Because the List interface implements the Collection interface, the List interface has all the common methods provided by the Collection interface. Because List is a List type, the List interface also provides some common methods suitable for itself. [Baidu]

The List interface provides a common method suitable for itself, which is related to the index. This is because the List set is a List type and objects are stored in a linear way. objects can be operated through the index of objects.

Common Implementation classes of the List interface include ArrayList and listlist. When using the List set, it is usually declared as the List type. During instantiation, It is instantiated as the ArrayList or listlist according to the actual needs, for example:

List <String> l = new ArrayList <String> (); // use the ArrayList class to instantiate a List set.

But! In the author's eclipse, if it is stated in the main function, it needs to be fully written, otherwise there will be a wonderful red wave line [I have been entangled here for a long time ....]

            java.util.List<String> list=new ArrayList<String>();        

But in public class, it is good to declare it directly.

1     2     static List<String> number=new ArrayList<String>();3     static List<String> word=new ArrayList<String>();4     

Here we declare two string lists, which are used to store numbers and strings in strings respectively.

To meet the requirements in the question, several custom functions are created.

Counting Function static void count (List <String> l)

1 static void count (List <String> l) {2 for (int I = 0; I <l. size (); I ++) {3 if (isnumber (l. get (I) {4 number. add (l. get (I); 5} else word. add (l. get (I); 6} 7 System. out. println ("NUMBERCOUNT:" + number. size (); 8 System. out. println ("WORDCOUNT:" + word. size (); 9} // count the number of strings and numbers

Here, List. add (String str) adds str to list. List. get (int index) is used to obtain objects.

There are several methods to determine whether a string is a number:

1. Use Character. isDigit (char) to judge

1 char num [] = str. toCharArray (); // converts a string to a character array 2 StringBuffer title = new StringBuffer (); // use the StringBuffer class, put a non-number in the title 3 StringBuffer hire = new StringBuffer (); // put the number in hire 4 for (int I = 0; I <num. length; I ++) {5 // determines whether the input number is a number or Character 6 if (Character. isDigit (num [I]) {converts a string to a Character, and then calls Character. the isDigit (char) method returns True if it is a number. Otherwise, False7 hire is returned. append (num [I]); // If you enter a number, assign it to hire} else {title. append (num [I]); // If the input is a character, assign it to title }}}

 

2. Use type conversion to determine

1 try {String str = "123abc"; 2 int num = Integer. valueOf (str); // forcibly convert a string to a number 3 return true; // if it is a number, return True4} catch (Exception e) {5 return false; // if an exception is thrown, False is returned}

 

3. Use regular expressions to determine

String str = "";boolean isNum = str.matches("[0-9]+");


// + Indicates one or more (for example, "3" or "225"), and * Indicates 0 or more ([0-9] *). (For example, "" or "1" or "22 "),? 0 or 1 ([0-9]?) (For example, "" or "7 ")
Ps: This method can only be used to determine whether it is a positive integer.

 

The second method is directly used in my program:

1 static boolean isnumber (String a) {2 try {3 Integer. parseInt (a); // numeric string conversion int-type number "123"-> 1234 return true; 5} catch (Exception e) {6 return false; 7} 8} // determines whether it is a number

Integer. parseInt (a) function. If a contains non-numbers, an exception is thrown. Return false.

The sorting function calls an sort built-in function under the collection object. [very easy to use !]

1 // Collections. sort sorting 2 Collections. sort (number); 3 Collections. sort (word );

In this way, number and word are directly converted into a list in ascending order.

There is actually another way to sort by calling the compare function.

Complete program:

1 import java. util. arrayList; 2 import java. util. collections; 3 import java. util. list; 4 import java. util. optional; 5 6 7 public class classtest {8 9 10 static List <String> number = new ArrayList <String> (); 11 static List <String> word = new ArrayList <String> (); 12 13 14 static void count (List <String> l) {15 for (int I = 0; I <l. size (); I ++) {16 if (isnumber (l. get (I) {17 number. add (l. get (I); 18} else word. add (l. g Et (I); 19} 20 System. out. println ("NUMBERCOUNT:" + number. size (); 21 System. out. println ("WORDCOUNT:" + word. size (); 22} // count the number of strings and numbers 23 24 25 static boolean isnumber (String a) {26 try {27 Integer. parseInt (a); // numeric string conversion int-type number "123"-> 12328 return true; 29} catch (Exception e) {30 return false; 31} 32} // determines whether the digit is 33 34 35 36 public static void main (String [] args) {37 38 System. out. println ("please input the s Tring "); 39 expect get = new expect (System. in); 40 String str = get. nextLine (); 41 System. out. println ("string is" + str); // The keyboard obtains the string 42 43 java. util. list <String> list = new ArrayList <String> (); // problem? 44 45 String [] text = str. split (""); 46 for (int I = 0; I <text. length; I ++) {47 list. add (text [I]); 48} // save to list49 50 51 classtest. count (list); 52 53 // Collections. sort sorting 54 Collections. sort (number); 55 Collections. sort (word); 56 System. out. println ("number sort:" + number); 57 System. out. println ("word sort:" + word); 58} 59 60}

The program is actually not difficult, but it's not familiar with java for a long time ......]

Program running result:

Okay ...... The baby continues to make the next question ......

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.