Software Test Experiment Two

Source: Internet
Author: User

(1) Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");

Filewoedcount:

Import Java.io.BufferedReader;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import java.util.*;

public class Filewordcount {
public static void Main (string[] args) {
try {
BufferedReader br = new BufferedReader (New FileReader ("D:\\test.txt"));
String s;
StringBuffer sb = new StringBuffer ();
while ((s = br.readline ()) = null) {
Sb.append (s);
}
map<string,integer> map = new hashmap<string, integer> ();
StringTokenizer st = new StringTokenizer (sb.tostring (), ",.! \ n");
while (St.hasmoretokens ()) {
String letter = St.nexttoken ();
int count;
if (map.get (letter) = = null) {
Count = 1;
} else {
Count = Map.get (letter). Intvalue () + 1;
}
Map.put (Letter,count);
}
set<wordentity> set = new treeset<wordentity> ();
For (String Key:map.keySet ()) {
Set.add (New Wordentity (Key,map.get (key));
}
Stitching strings yourself, outputting the desired string format
System.out.println ("Output form one:");
for (iterator<wordentity> it = Set.iterator (); It.hasnext ();) {
wordentity w = it.next ();
System.out.println ("Word:" + w.getkey () + "occurrences:" + w.getcount ());
}
Print the Wordentity object directly, implement the output effect we want, just rewrite the ToString () method in the Wordentity class
System.out.println ("Output form II:");
for (iterator<wordentity> it = Set.iterator (); It.hasnext ();) {
wordentity w = it.next ();
System.out.println (w);
}
We can control only the top three of the output.
System.out.println ("Output Form III:");
int count = 1;
for (iterator<wordentity> it = Set.iterator (); It.hasnext ();) {
wordentity w = it.next ();
System.out.println ("+ count +" named Word: "+ w.getkey () +" occurs as many times as: "
+ W.getcount ());
if (count = = 3)//When output 3 out of loop
Break
count++;
}
} catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File not found! ");
} catch (IOException e) {
SYSTEM.OUT.PRINTLN ("file read abnormal!") ");
}
}

}

Filewordcounttest:

Import Org.junit.After;
Import Org.junit.AfterClass;
Import Org.junit.Before;
Import Org.junit.BeforeClass;


public class Filewordcounttest {

@BeforeClass
public static void Setupbeforeclass () throws Exception {
}

@AfterClass
public static void Teardownafterclass () throws Exception {
}

@Before
public void SetUp () throws Exception {
}

@After
public void TearDown () throws Exception {
}

}

Wordentity:

public class Wordentity implements comparable<wordentity> {
Private String key;
Private Integer count;
Public wordentity (String key, Integer count) {
This.key = key;
This.count = count;
}
public int compareTo (wordentity o) {
int cmp = Count.intvalue ()-O.count.intvalue ();
return (CMP = = 0 Key.compareto (o.key):-CMP);
Just add a minus sign here to decide whether to sort ascending or descending-cmp in descending order, CMP ascending
Because TreeSet will call Workformap's CompareTo method to determine its own sort
}

@Override
Public String toString () {
Return key + "occurrences are:" + count;
}

Public String GetKey () {
Return key;
}

Public Integer GetCount () {
return count;
}

}

Software Test Experiment Two

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.