Advertisement
Guest User

tehtava1

a guest
May 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import java.awt.List;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4.  
  5. public class Tehtava1 {
  6.  
  7. public static void main(String[] args) {
  8. trimAndSaveSmileys("Moi! :) Mitä (ihmettä?) sinulle kuuluu tänään (; =D D= :/ :{");
  9. }
  10. public static ArrayList<String> trimAndSaveSmileys(String str){
  11. //split every word
  12. String [] strings = str.split(" ");
  13. //Add all combinations that are considered as smileys
  14. ArrayList<String> allowedSmileys = new ArrayList<>(Arrays.asList(
  15. ":)",
  16. ";)",
  17. "(:",
  18. "(;",
  19. "=D",
  20. "D=",
  21. ":/",
  22. ":{")
  23. );
  24. ArrayList<String> fixedStrList = new ArrayList<String>();
  25. boolean smileyFound;
  26. for(int i=0;i<strings.length;i++){
  27. //check if word is one of allowed smileys
  28. smileyFound = false;
  29. for (String smiley : allowedSmileys) {
  30. if(strings[i].equals(smiley)){
  31. smileyFound = true;
  32. }
  33. }
  34. if(smileyFound == true){
  35. //smiley no need to trim
  36. fixedStrList.add(strings[i]);
  37. } else {
  38. //not smiley so replace everything except numbers and letters
  39. strings[i] = strings[i].replaceAll("[^a-zA-Z0-9äÄöÖ]", "");
  40. fixedStrList.add(strings[i]);
  41. }
  42.  
  43. }
  44. System.out.println(fixedStrList);
  45. return fixedStrList;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement