Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays; //importowanie metod działania na tablicach
- public class lab2home {
- public static void main(String[] args)
- {
- String sentence = "How are you, are you ok? If not please say something. I would like to ask you how was your day. What are your plans for the rest of the week? Any good plans for the weekend? Just-in-case if we don't see each other: have a joyful day!";
- String[] words = sentence.split("\\s|\\W"); //polecam do ogarnięcia regexu
- //http://www.vogella.com/articles/JavaRegularExpressions/article.html
- int count = 0; //licznik wystąpień
- for (String singleWord : words) //pętla typu foreach :)
- {
- count = 0;
- for (int i=0; i<words.length; i++)
- {
- if (singleWord.equals(words[i]))
- {
- count++;
- }
- }
- if(singleWord.equals(""))
- {//pozbywanie się drukowania pustych linii
- }
- else
- {
- System.out.println(singleWord + " " + count);
- }
- }
- ////////////Sortowanie//////////
- System.out.println();
- System.out.println("--Sortowanie--");
- for(int i=0; i<words.length; i++)
- {
- words[i] = words[i].toLowerCase();
- }
- Arrays.sort(words);
- for(int i=0; i<words.length; i++)
- {
- if(i==0)
- {
- System.out.println(words[0]);
- }
- if(i>0)
- {
- if(words[i-1].equals(words[i]))
- {//pomijamy bo się powtarza
- }
- else
- {
- System.out.println(words[i]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment