Advertisement
Guest User

night

a guest
Jan 4th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class test {
  5.  
  6.     public static void main( String [] args )
  7.     {
  8.         Scanner input = new Scanner(System.in);
  9.  
  10.         String [] alpha = {"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","1","2","3","4","5","6","7","8","9","0"," "};
  11.         String [] dottie = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "|"};
  12.  
  13.         System.out.println("Enter English to convert to English or Morse to convert to Morse:");
  14.  
  15.         String ans = input.nextLine();
  16.  
  17.         if(ans.equals("English"))
  18.         {
  19.             System.out.println( "Please enter the text you would like to convert to Morse Code: ");
  20.             String english = input.nextLine();
  21.  
  22.             char [] translates = (english.toLowerCase()).toCharArray();
  23.             System.out.println(toMorse(translates, dottie));
  24.  
  25.  
  26.         }
  27.         else if(ans.equals("Morse"))
  28.         {
  29.             System.out.println( "Please enter the text you would like to convert to English (separate words with '|'):");
  30.             String code = input.nextLine();
  31.  
  32.             String[] translates = code.split("[|]", 0);
  33.             System.out.println(toEnglish(translates, dottie, alpha));
  34.  
  35.         }
  36.         else
  37.             System.out.println("Invalid input, please try again.");
  38.     }
  39.  
  40.     public static String toMorse(char [] translates, String [] dottie)
  41.     {          
  42.         String morse = "";
  43.         for (int j = 0; j < translates.length; j++)
  44.         {
  45.             char a = translates[j]; //.valueOf(j);
  46.             if(Character.isLetter(a))
  47.             {
  48.                 morse += dottie[a - 'a'];
  49.             }
  50.         }
  51.         return morse;
  52.     }
  53.  
  54.     // the current solution won't need alpha; but the other one (commented out), need it
  55.     // for the sake of Something, I have kept the signature with 3 args.
  56.     // uncomment the s+=alpha[k] line, comment the s+=Character... line and you should
  57.     // have the solution using the alpha array to convert back. See explanation in the answer.
  58.     public static String toEnglish(String [] translates, String  [] dottie, String[] alpha)
  59.     {
  60.         String s = "";
  61.         for (int n = 0; n < translates.length; n++)
  62.         {
  63.             String a = translates[n];
  64.             int k = Arrays.asList(dottie).indexOf(a);
  65.             if (k >= 0) {
  66.                 //s += alpha[k];
  67.                 s += Character.toChars(k + 'a')[0];
  68.             }
  69.         }
  70.         return s;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement