Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class MorseCode{
  4. static String[] MorseAlpha = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
  5. static String[] MorseDigit = {"-----",".----","..--","...--","....-",".....","-....","--...","--..","---."};
  6. public static void main(String[] args) {
  7. String s = "Welcome to the North Seattle Computer Science Club";
  8. String morse = convertTextToMorse(s);//To Morse
  9. System.out.println(morse);//Print it
  10. String test = convertMorseToString(morse);//Back to Text
  11. System.out.println(test);//Print it
  12. }
  13.  
  14. //************************FROM TEXT TO MORSE*******************************/
  15. /**
  16. * Converts a single line into morse code.
  17. * @param s The string you want converted
  18. * @throws IllegalArgumentException if there is something in the string it can't convert.(Not Alpha||Not Digit||Not Space.)
  19. */
  20. public static String convertTextToMorse(String s){
  21. String output = "";
  22. for(int i = 0; i < s.length(); i++){
  23. char letter = s.charAt(i);
  24. letter = Character.toUpperCase(letter);
  25. if(canConvertToMorse(letter)){
  26. output += " " + convertCharToMorse(letter);
  27. }else{
  28. throw new IllegalArgumentException("\""+ letter +"\" Can not be converted to morse code");
  29. }
  30. }
  31. return output;
  32. }
  33. /**
  34. * Converts a singular Char to morse
  35. * @param c the char you want to convert
  36. * @return the string value of that char in morse code
  37. */
  38. public static String convertCharToMorse(Character c){
  39. if(Character.isLetter(c)){
  40. int index = (int)(c - 65);
  41. return MorseAlpha[index];
  42. }else if(c == ' '){
  43. return "/";
  44. }else{
  45. int index = (int)(c-48);
  46. return MorseDigit[index];
  47. }
  48. }
  49.  
  50. /**
  51. * Checks if the char can be converted to Morse with the current data
  52. * @param letter the char you want to check
  53. * @return true if it can false if it can't
  54. */
  55. public static boolean canConvertToMorse(Character letter){
  56. return (Character.isLetter(letter) || Character.isDigit(letter) || letter == ' ');
  57. }
  58.  
  59. //************************FROM MORSE TO TEXT*******************************/
  60. /**
  61. * Convert from Morse to String.
  62. * @param s the String of morse code you want to convert.
  63. * @return a String of Text in all caps that represents the Morse Code.
  64. */
  65. public static String convertMorseToString(String s){
  66. String output = "";
  67. Scanner processing = new Scanner(s);
  68. while(processing.hasNext()){
  69. //Grab the next letter
  70. String letter = processing.next();
  71. //Instantiate so I can use later.
  72. Character addToOutput;
  73. /*Check if it is a space. While this is rare if it is true it saves
  74. me alot of time because then I don't have to call all the other methods*/
  75. if(letter.equals("/")){
  76. addToOutput = ' ';
  77. }else{//If not a space
  78. //Find the index in the array
  79. /* My Arrays are setup in ascending order just like they are on the
  80. * unicode table. So if the letter is A then I will get back the
  81. * index that it is in. Which is 0. The Unicode value for A is 65
  82. * 65+0 = 65. So if I create a char of value 65 it will convert to a
  83. * 'A' when printed or added to a string.
  84. * If I get a negative number then I know it is in the Digits Array.
  85. * I change the sign again and then add 48 to it because 48 is the
  86. * Unicode value of 0.
  87. *
  88. */
  89. int index = convertToText(letter);
  90. if(index < 0){
  91. index *= -1;
  92. addToOutput = (char) (48 + index);
  93. }else{
  94. addToOutput = (char) (65 + index);
  95. }
  96. }
  97. output += Character.toString(addToOutput);
  98. }
  99. processing.close();
  100. return output;
  101. }
  102.  
  103. /**
  104. * Converts a single letter to morse code.
  105. * @param letter The letter you want to convert to Morse
  106. * @return the index value of that letter in the arrays. The number will be negative
  107. * if it is in the MorseDigits array.
  108. * @throws IllegalArgumentException if the number can not be converted with the current data.
  109. */
  110. public static int convertToText(String letter){
  111. for(int i = 0; i < MorseAlpha.length; i++){
  112. if(MorseAlpha[i].equals(letter)){
  113. return i;
  114. }
  115. if(i < MorseDigit.length){
  116. if(MorseDigit[i].equals(letter)){
  117. return i*-1;
  118. }
  119. }
  120. }
  121. throw new IllegalArgumentException("\""+ letter +"\" Can not be converted to morse code");
  122. }
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement