Advertisement
anhit92

DemTu

Nov 17th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package Demsotu;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author Anh-Kool
  17.  */
  18. public class Demtu {
  19.  
  20.     public static void main(String[] args) throws IOException{
  21.         HashMap<String, Integer> hm = new HashMap<String, Integer>();
  22.         ArrayList<String> al = new ArrayList<String>();
  23.         String duongdan;
  24.         Scanner s = new Scanner(System.in);
  25.         System.out.print("Mơi ban nhap duong dan tap tin: ");
  26.         duongdan = s.nextLine();
  27.         FileReader fr = new FileReader(duongdan);
  28.         BufferedReader br = new BufferedReader(fr);
  29.         String dong;
  30.         while ((dong = br.readLine()) != null) {
  31.             al.add(dong);
  32.         }
  33.         for (int i = 0; i < al.size(); i++) {
  34.             String[] arr = al.get(i).split(" ");
  35.             for (int j = 0; j < arr.length; j++) {
  36.                 String str = arr[j];
  37.                 if(!hm.containsKey(str)){
  38.                     hm.put(str, 1);
  39.                 }else{
  40.                     int n = hm.get(str).intValue();
  41.                     n++;
  42.                     hm.put(str, n);
  43.                 }
  44.             }
  45.         }
  46.         System.out.println("Cac tu dem duoc la: ");
  47.         for (String k : hm.keySet()) {
  48.             Integer v = hm.get(k);
  49.             System.out.println(k+ " : "+ v);
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement