piowit

java.lab2home

Oct 9th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. import java.util.Arrays; //importowanie metod działania na tablicach
  2. public class lab2home {
  3.  
  4.     public static void main(String[] args)
  5.     {
  6.        
  7.         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!";
  8.        
  9.         String[] words = sentence.split("\\s|\\W"); //polecam do ogarnięcia regexu
  10.                                                     //http://www.vogella.com/articles/JavaRegularExpressions/article.html
  11.         int count = 0; //licznik wystąpień
  12.         for (String singleWord : words) //pętla typu foreach :)
  13.         {
  14.            
  15.             count = 0;
  16.             for (int i=0; i<words.length; i++)
  17.             {              
  18.                 if (singleWord.equals(words[i]))
  19.                 {
  20.                     count++;
  21.                 }
  22.             }
  23.             if(singleWord.equals(""))
  24.             {//pozbywanie się drukowania pustych linii
  25.             }
  26.             else
  27.             {
  28.             System.out.println(singleWord + " " + count);              
  29.             }
  30.         }
  31.        
  32.         ////////////Sortowanie//////////
  33.         System.out.println();
  34.         System.out.println("--Sortowanie--");
  35.        
  36.         for(int i=0; i<words.length; i++)
  37.         {
  38.             words[i] = words[i].toLowerCase();
  39.         }
  40.        
  41.         Arrays.sort(words);
  42.        
  43.         for(int i=0; i<words.length; i++)
  44.         {
  45.             if(i==0)
  46.             {
  47.                 System.out.println(words[0]);
  48.             }
  49.             if(i>0)
  50.             {
  51.                 if(words[i-1].equals(words[i]))
  52.                 {//pomijamy bo się powtarza               
  53.                 }
  54.                 else
  55.                 {
  56.                     System.out.println(words[i]);
  57.                 }
  58.             }
  59.         }
  60.        
  61.        
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment