An object-oriented approach to the "list feature in Java" program for the last "simple character and numeric statistics on strings"

Source: Internet
Author: User

The procedure in the previous essay, after thinking, found that using too many static functions and not incorporating object-oriented ideas made a change:

1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 Importjava.util.List;4 ImportJava.util.Scanner;5 6 7  Public classClasstest {8     9List<string> number=NewArraylist<string>();TenList<string> word=NewArraylist<string>(); One      A classtest () {}; -     voidCount (list<string>l) { -          for(intI=0;i<l.size (); i++){ the             if(Isnumber (L.get (i))) { - Number.add (L.get (i)); -}ElseWord.add (L.get (i)); -         } +System.out.println ("Numbercount:" +number.size ()); -System.out.println ("WORDCOUNT:" +word.size ()); +}//count the number of strings and numbers A      at      -     BooleanIsnumber (String a) { -          Try { -Integer.parseint (a);//numeric string conversion int type number "123"->123 -                return true; -}Catch(Exception e) { in                return false; -               } to}//determine whether a number +      -      the      *      Public Static voidMain (string[] args) { $         classtest s=new  classtest (); Panax NotoginsengSYSTEM.OUT.PRINTLN ("Please input the string"); -Scanner get=NewScanner (system.in); theString str=get.nextline (); +System.out.println ("string is" +str);//Keyboard Get string A          theList<string> list=NewArraylist<string> ();//problem?  +          -string[] Text = Str.split (""); $          for(inti=0;i<text.length;i++){ $ List.add (Text[i]); -}//Deposit List -          the          - S.count (list);Wuyi      the     //Collections.sort Sort - Collections.sort (s.number); Wu Collections.sort (s.word); -System.out.println ("number sort:" + s.number); AboutSystem.out.println ("word Sort:" + S.word); $     } -  -}

The bold code is for modifying the added part. This completes the encapsulation of a program.

An object-oriented approach to the "list feature in Java" program for the last "simple character and numeric statistics on strings"

Related Article

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.