Java identifies the number of occurrences of a word in an article _java

Source: Internet
Author: User
Tags readline

This article illustrates how Java recognizes the number of occurrences of a word in an article. Share to everyone for your reference. Specifically as follows:

1. Java Code:

Import Java.io.DataInputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.util.StringTokenizer;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
    public class Select {public static void main (string[] args) {int num = 0;
    Definition: Byte read stream FileInputStream fis;
      The try {//here path needs to be modified according to the circumstances FIS = new FileInputStream ("H:\\tankwar1.9\\src\\tank.java");
      DataInputStream dis = new DataInputStream (FIS);
      String line = null; while (line = Dis.readline ())!= null) {//Create character Parser StringTokenizer St=new StringTokenizer (line, "!&" ()
         {}+-= ':;<>/");
           while (St.hasmoretokens ()) {String string=st.nexttoken ();
        if (String.Equals ("if")) {num++}}
      ;
    } catch (FileNotFoundException e) {e.printstacktrace ();
    catch (IOException e) {e.printstacktrace ();
  } System.out.println (num);

 }
}

2. Select.java:

Import Java.io.DataInputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.util.StringTokenizer;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
    public class Select {public static void main (string[] args) {int num = 0;
    Definition: Byte read stream FileInputStream fis;
      try {fis = new FileInputStream ("H:\\tankwar1.9\\src\\tank.java");
      DataInputStream dis = new DataInputStream (FIS);
      String line = null; while (line = Dis.readline ())!= null) {//Create character resolution class StringTokenizer St=new StringTokenizer (line, "!&" ()
         {}+-= ':;<>/");
           while (St.hasmoretokens ()) {String string=st.nexttoken ();
        if (String.Equals ("if")) {num++}}
      ;
    } catch (FileNotFoundException e) {e.printstacktrace ();
    catch (IOException e) {e.printstacktrace ();
  } System.out.println (num);

 }
}

3. Stringtokenizerdemo.java:

Import java.util.*; public class Stringtokenizerdemo {public static void main (string[] args) {String str1 = "Hello world!
      This is Java code,stringtokenizer Demo. "; Declares and initializes the string str1 string str2 = "How do I use StringTokenizer?"
      StringTokenizer? ";
      Declares and initializes the string str2 stringtokenizer strT1 = new StringTokenizer (str1, ",.!"); Creates an object strT1 for the StringTokenizer class and constructs a string STR1 parser//To spaces, ",", "." and "!"
      As delimiter stringtokenizer strT2 = new StringTokenizer (str2, "?"); Creates an object strT2 for the StringTokenizer class and constructs a string STR2 parser//To spaces and "?"
      As delimiter int num1 = Strt1.counttokens ();
      Gets the number of language symbols in the string str1 int num2 = Strt2.counttokens (); Gets the number of language symbols in the string str2 System.out.println ("str1 has +num1+" words.)
      They are: ");
          while (Strt1.hasmoretokens ()) {//using loops to get the next language symbol in the string str1 and output string str = Strt1.nexttoken ();
      System.out.print ("\" "+str+"); } System.out.println ("\NSTR2 has" +num2+ words.)
      They are: "); WhileStrt2.hasmoretokens ()) {//Use loops to get the next language symbol in the string str2 and output string str = Strt2.nexttoken ();
      System.out.print ("\" "+str+");

 }
  }
}

I hope this article will help you with your Java programming.

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.