Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Collection {
  4. String[] strings;
  5.  
  6. private static Collection ourInstance = new Collection();
  7.  
  8. public static Collection getInstance() {
  9. return ourInstance;
  10. }
  11.  
  12. private Collection() {
  13. }
  14.  
  15. public void readConsole(){
  16. System.out.println("Enter String:");
  17. Scanner scanner = new Scanner(System.in);
  18. String s = scanner.nextLine();
  19.  
  20. strings = s.split("\\s");
  21. }
  22.  
  23. public String sameLetters(){
  24. try {
  25. String lettersOutput = new String();
  26. for (int i = 0; i < strings[0].length(); i++) {
  27. char c = strings[0].charAt(i);
  28. if (strings[1].indexOf(c) != -1 && strings[2].indexOf(c) != -1 && lettersOutput.indexOf(c) == -1)
  29. lettersOutput += strings[0].charAt(i);
  30. }
  31. return lettersOutput;
  32. }
  33. catch (IndexOutOfBoundsException e){
  34. return "There are not enough strings to find similar letters";
  35. }
  36. }
  37.  
  38. public String[] getStrings(){
  39. return strings;
  40. }
  41.  
  42. @Override
  43. public String toString(){
  44. String result = new String();
  45. for (int i = 0; i < strings.length; i++){
  46. result += strings[i] + "\n";
  47. }
  48. return result;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement