Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- public class Odczyt {
- private String sciezkaPliku = "C://Users//mad//Desktop//plik2.txt";
- private String zadanie="";
- private int licznik = 0;
- public void funkcja(){
- File plik = new File(sciezkaPliku);
- String[] s = null;
- HashMap<String, Integer> mapa = new HashMap<String, Integer>();
- try {
- Scanner in = new Scanner(plik);
- while(in.hasNextLine())
- {
- zadanie = in.nextLine();
- System.out.println("Zdanie : " + zadanie);
- s = zadanie.split("\\s+");
- for(int i=0;i<s.length;i++)
- {
- System.out.println("element " + i + " = " + s[i]);
- mapa.put(s[i], licznik);
- }
- }
- in.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //klucz = słowo
- //wartosc = ilosc wystapienie
- for (String slowo : s){
- if(mapa.containsKey(slowo)){
- Integer wartosc = mapa.get(slowo);
- mapa.put(slowo, wartosc + 1);
- } else
- {
- mapa.put(slowo, 1);
- }
- }
- Map.Entry<String, Integer> max = null;
- for (Entry<String,Integer> entry : mapa.entrySet()){
- if(max == null || entry.getValue() > max.getValue()){
- max = entry;
- }
- }
- System.out.println("Rozmiar mapy : " + mapa.size());
- System.out.println("Najwieksza wartosc to = " + max.getKey() + " i wynosi = " + max.getValue());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment