Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. public class rektop
  4. {
  5.  
  6. public static String[] charCreator(String s)
  7. {
  8. String[] charHolder = new String[s.length()];
  9. for(int i=0; i < charHolder.length; i++)
  10. {
  11. Character currentChar = s.charAt(i);
  12. String charStringed = String.valueOf(currentChar);
  13. charHolder[i] = charStringed;
  14. }
  15.  
  16. return charHolder;
  17. }
  18.  
  19. public static String[] morseCreator(ArrayList<String> s)
  20. {
  21. String[] morse = new String[s.size()];
  22. for (int i=0; i<s.size(); i++)
  23. {
  24. String currentWordAt = s.get(i);
  25. morse[i] = currentWordAt;
  26. System.out.println(currentWordAt);
  27. }
  28.  
  29. return morse;
  30. }
  31.  
  32.  
  33. public static String[] morseCode(String[] getChars)
  34. {
  35. String[] finalAnswer = new String[getChars.length];
  36. for(int i=0; i < getChars.length; i++)
  37. {
  38.  
  39. switch(getChars[i])
  40. {
  41.  
  42. case "a":
  43. String a = ".- ";
  44. finalAnswer[i] = a;
  45. break;
  46. case "b":
  47. String b = "-... ";
  48. finalAnswer[i] = b;
  49. break;
  50. case "c":
  51. String c = "-.-. ";
  52. finalAnswer[i] = c;
  53. break;
  54. case "d":
  55. String d = "-.. ";
  56. finalAnswer[i] = (d);
  57. break;
  58. case "e":
  59. String e = ". ";
  60. finalAnswer[i] = (e);
  61. break;
  62. case "f":
  63. String f = "..-. ";
  64. finalAnswer[i] = (f);
  65. break;
  66. case "g":
  67. String g = "--. ";
  68. finalAnswer[i] = (g);
  69. break;
  70. case "h":
  71. String h = ".... ";
  72. finalAnswer[i] = (h);
  73. break;
  74. case "i":
  75. String ii = ".. ";
  76. finalAnswer[i] = (ii);
  77. break;
  78. case "j":
  79. String j = ".--- ";
  80. finalAnswer[i] = (j);
  81. break;
  82. case "k":
  83. String k = "-.- ";
  84. finalAnswer[i] = (k);
  85. break;
  86. case "l":
  87. String l = ".-.. ";
  88. finalAnswer[i] = (l);
  89. break;
  90. case "m":
  91. String m = "-- ";
  92. finalAnswer[i] = (m);
  93. break;
  94. case "n":
  95. String n = "-. ";
  96. finalAnswer[i] = (n);
  97. break;
  98. case "o":
  99. String o = "--- ";
  100. finalAnswer[i] = (o);
  101. break;
  102. case "p":
  103. String p = ".--. ";
  104. finalAnswer[i] = (p);
  105. break;
  106. case "q":
  107. String q = "--.- ";
  108. finalAnswer[i] = (q);
  109. break;
  110. case "r":
  111. String r = ".-. ";
  112. finalAnswer[i] = (r);
  113. break;
  114. case "s":
  115. String s = "... ";
  116. finalAnswer[i] = (s);
  117. break;
  118. case "t":
  119. String t = "- ";
  120. finalAnswer[i] = (t);
  121. break;
  122. case "u":
  123. String u = "..- ";
  124. finalAnswer[i] = (u);
  125. break;
  126. case "v":
  127. String v = "...- ";
  128. finalAnswer[i] = (v);
  129. break;
  130. case "w":
  131. String w = ".-- ";
  132. finalAnswer[i] = (w);
  133. break;
  134. case "x":
  135. String x = "-..- ";
  136. finalAnswer[i] = (x);
  137. break;
  138. case "y":
  139. String y = "-.-- ";
  140. finalAnswer[i] = (y);
  141. break;
  142. case "z":
  143. String z = "--.. ";
  144. finalAnswer[i] = (z);
  145. break;
  146. case " ":
  147. String space = " ";
  148. finalAnswer[i] = space;
  149. break;
  150. default:
  151. System.out.println("Not a letter!");
  152. break;
  153. }
  154. }
  155. return finalAnswer;
  156. }
  157.  
  158.  
  159. public static void main(String[] args)
  160. {
  161. boolean run = true;
  162. while(run == true)
  163. {
  164. String blankSpace = " ";
  165. Scanner choiceSelection = new Scanner(System.in);
  166. System.out.println("(1) Translate into morse code");
  167. System.out.println("(2) Translate from morse code");
  168. System.out.println("(3) Exit Program");
  169.  
  170. String choice = choiceSelection.next();
  171.  
  172. while (choice.equals("2") && run == true)
  173. {
  174. System.out.println("You chose to translate from morse code!");
  175. System.out.println("Enter morse code in, words separated by spaces.");
  176. Scanner getInput = new Scanner(System.in);
  177. ArrayList<String> wordList = new ArrayList<String>();
  178. while(getInput.hasNext())
  179. {
  180. String currentWord = getInput.next();
  181. wordList.add(currentWord);
  182. System.out.println("currentWord=" + currentWord);
  183. wordList.add(blankSpace);
  184. System.out.println(wordList);
  185.  
  186. }
  187. getInput.close();
  188. System.out.println(wordList);
  189. System.out.println("test");
  190.  
  191. String[] finalAns = morseCreator(wordList);
  192. System.out.println("size" + finalAns.length);
  193. for(int i=0; i<finalAns.length; i++)
  194. {
  195. System.out.println("gggg" + finalAns[i]);
  196. }
  197. run = false;
  198. break;
  199.  
  200.  
  201. }
  202. choiceSelection.close();
  203. System.out.println("1/2/3?");
  204. Scanner newChoiceSelection = new Scanner(System.in);
  205. String newChoice = newChoiceSelection.next();
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. if (choice.equals("1"))
  217. {
  218. System.out.println("You chose to translate into morse code!");
  219. System.out.println("Enter Word: ");
  220. @SuppressWarnings("resource")
  221. Scanner wordGet = new Scanner(System.in);
  222. String word = wordGet.nextLine();
  223. String[] charString = (charCreator(word));
  224. String[] finalOutput = morseCode(charString);
  225. for (int i=0; (i <= finalOutput.length-1); i++)
  226. System.out.print(finalOutput[i]);
  227.  
  228. System.out.println("Would you like to continue?(Y//N)");
  229. @SuppressWarnings("resource")
  230. Scanner grabAns = new Scanner(System.in);
  231. String ans = grabAns.nextLine();
  232. if (ans.equalsIgnoreCase("y"))
  233. continue;
  234.  
  235. else if (ans.equalsIgnoreCase("n"))
  236. {
  237. System.out.println("Thanks for using the program!");
  238. run = false;
  239. continue;
  240. }
  241. while(!ans.equals("y") && (!ans.equals("n")))
  242. {
  243. @SuppressWarnings("resource")
  244. Scanner newScanner = new Scanner(System.in);
  245. System.out.println("Invalid choice, select a correct one!");
  246. System.out.println("Would you like to continue?(Y//N)");
  247. ans = newScanner.nextLine();
  248. }
  249. if (ans.equalsIgnoreCase("n"))
  250. {
  251. System.out.println("Thanks for using the program!");
  252. run = false;
  253. continue;
  254. }
  255.  
  256. }
  257. if (choice.equals("3"))
  258. {
  259. run = false;
  260. System.out.println("Thanks for using the program!");
  261. continue;
  262. }
  263.  
  264.  
  265. }
  266.  
  267. }
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement