Advertisement
Dubwyn

Text to Morse

Nov 13th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1.  public static void main(String[] args){
  2.             Scanner input = new Scanner(System.in);
  3.            
  4.             char[] letters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',};
  5.             String[] morseLetters = { "    ", ". ___", "___ . . .", "___ . ___ .", "___ . .", ".", ". . ___ .", "___ ___ .", ". . . .", ". .", ". ___ ___ ___", "___ . ___", ". ___ . .",  "___ ___", "___ .", "___ ___ ___", ". ___ ___ .", "___ ___ . ___", ". ___ .", ". . .", "_", ". . ___", ". . . ___", ". ___ ___", "___ . . ___", "___ . ___ ___", "___ ___ . ."};
  6.          
  7.             String textToChange = "";
  8.             String newText = "";
  9.             System.out.println("Enter text you want to change to Morse code");
  10.             textToChange = input.nextLine();
  11.            
  12.             textToChange = textToChange.toLowerCase();
  13.              
  14.             for (int i = 0; i < textToChange.length(); i++) {
  15.               for (short j = 0; j < 26; j++) {
  16.                 if (textToChange.charAt(i) == letters[j]) {
  17.                   newText += morseLetters[j];
  18.                   newText += "   ";
  19.          
  20.                   break;
  21.                 }    
  22.               }
  23.             }
  24.            
  25.             System.out.println("Text in Morse Code");
  26.             System.out.println(newText);
  27.           }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement