Advertisement
Valeri12580

Song Encryption

Apr 10th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class SongEncryption {
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.  
  12. String command="";
  13.  
  14. while (!"end".equals(command=reader.readLine())) {
  15. String[]input=command.split("[:]");
  16. String artist=input[0];
  17. String song=input[1];
  18.  
  19. Pattern patternForArtist=Pattern.compile("^[A-Z][a-z ']+$");
  20. Matcher matcherForArtist=patternForArtist.matcher(artist);
  21. Pattern patternForSong=Pattern.compile("^[A-Z ]+$");
  22. Matcher matcherForSong=patternForSong.matcher(song);
  23.  
  24. if(!(matcherForArtist.find()&& matcherForSong.find())){
  25. System.out.println("Invalid input!");
  26. continue;
  27. }
  28.  
  29. int key=artist.length();
  30. StringBuilder crypted= new StringBuilder();
  31. for (int i = 0; i <1; i++) {
  32. for (int j = 0; j <command.indexOf(":"); j++) {
  33. char currentChar=command.charAt(j);
  34. if(String.valueOf(currentChar).equals(" ")||String.valueOf(currentChar).equals("'")){
  35. if(String.valueOf(currentChar).equals(" ")){
  36. crypted.append(" ");
  37. }else{
  38. crypted.append("'");
  39. }
  40.  
  41. continue;
  42.  
  43. }
  44. if (!(currentChar+key>122)) {
  45. crypted.append(String.valueOf(Character.toChars(currentChar + key)));
  46. }else{
  47. int tempKey=currentChar+key-122;
  48. crypted.append(String.valueOf(Character.toChars('a' + tempKey - 1)));
  49. }
  50. }
  51. crypted.append("@");
  52. for (int j = command.indexOf(":")+1; j <command.length(); j++) {
  53. char currentChar=command.charAt(j);
  54. if(String.valueOf(currentChar).equals(" ")||String.valueOf(currentChar).equals("'")){
  55. if(String.valueOf(currentChar).equals(" ")){
  56. crypted.append(" ");
  57. }else{
  58. crypted.append("'");
  59. }
  60.  
  61. continue;
  62.  
  63. }
  64. if (!(currentChar+key>90)) {
  65. crypted.append(String.valueOf(Character.toChars(currentChar + key)));
  66. }else{
  67. int tempKey=currentChar+key-90;
  68.  
  69.  
  70. crypted.append(String.valueOf(Character.toChars('A' + tempKey - 1)));
  71. }
  72.  
  73. }
  74. System.out.println(String.format("Successful encryption: %s", crypted.toString()));
  75.  
  76.  
  77. }
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement