madzik4113

xtrf rozmowa2

Jul 29th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class Odczyt {
  10.    
  11.     private String sciezkaPliku = "C://Users//mad//Desktop//plik2.txt";
  12.     private String zadanie="";
  13.     private int licznik = 0;
  14.  
  15.    
  16.     public void funkcja(){
  17.        
  18.         File plik = new File(sciezkaPliku);
  19.         String[] s = null;
  20.         HashMap<String, Integer> mapa = new HashMap<String, Integer>();
  21.        
  22.        
  23.         try {
  24.             Scanner in = new Scanner(plik);
  25.             while(in.hasNextLine())
  26.             {
  27.                 zadanie = in.nextLine();
  28.                 System.out.println("Zdanie : " + zadanie);
  29.                 s = zadanie.split("\\s+");
  30.                 for(int i=0;i<s.length;i++)
  31.                 {
  32.                    
  33.                     System.out.println("element " + i + " = " + s[i]);
  34.                     mapa.put(s[i], licznik);
  35.                 }
  36.                
  37.             }
  38.             in.close();
  39.         } catch (FileNotFoundException e) {
  40.             // TODO Auto-generated catch block
  41.             e.printStackTrace();
  42.         }
  43.    
  44.         //klucz = słowo
  45.         //wartosc = ilosc wystapienie
  46.        
  47.         for (String slowo : s){
  48.             if(mapa.containsKey(slowo)){
  49.                 Integer wartosc = mapa.get(slowo);
  50.                 mapa.put(slowo, wartosc + 1);      
  51.             } else
  52.             {
  53.                 mapa.put(slowo, 1);
  54.             }
  55.         }
  56.        
  57.         Map.Entry<String, Integer> max = null;
  58.         for (Entry<String,Integer> entry : mapa.entrySet()){
  59.             if(max == null || entry.getValue() > max.getValue()){
  60.                 max = entry;
  61.             }
  62.         }
  63.         System.out.println("Rozmiar mapy : " + mapa.size());   
  64.         System.out.println("Najwieksza wartosc to = " + max.getKey() + " i wynosi = " + max.getValue());
  65.    
  66.        
  67.        
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment