Source code for submission s650

Fs.java

  1. import java.io.*;
  2.  
  3. public class Fs{
  4. static String line; //doesn't need resetting
  5. static String morse =""; //reset every line
  6. static int[] key; //reset every line
  7. static int counter = 0; //reset every line
  8. static String[] library= {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-", ".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","..--",".-.-","---.","----"};
  9. static int xlen;
  10. static String decode = "";
  11. static String answer = "";
  12. static int totallen = 0;
  13. public static void main(String[] args) throws IOException{
  14.  
  15. while((line = reader.readLine()) != null){
  16.  
  17. morse = "";
  18. counter = 0;
  19. key = new int[line.length()];
  20.  
  21. for(int i = 0; i<line.length(); i++){
  22. addMorse(line.charAt(i));
  23. }
  24. answer = "";
  25. totallen = 0;
  26. calculate();
  27. }
  28. }
  29.  
  30. static void calculate(){
  31. for(int i = 0; i<key.length;i++){
  32. xlen = key[key.length-(1+i)];
  33. for(int j = 0; j<xlen; j++){
  34. decode += morse.charAt(totallen);
  35. totallen++;
  36.  
  37. }
  38. answer += decodeMorse(decode);
  39. decode = "";
  40.  
  41.  
  42.  
  43. }
  44. System.out.println(answer);
  45. }
  46.  
  47. static String decodeMorse(String morse){
  48. for(int i = 0; i<library.length; i++){
  49. if(morse.equals(library[i])){
  50. if (i==26)
  51. return "_";
  52. else if (i==27)
  53. return ",";
  54. else if (i==28)
  55. return ".";
  56. else if (i==29)
  57. return "?";
  58. else{
  59. String returnVal = Character.valueOf(((char)('A'+i))).toString();
  60. return( returnVal);
  61. }
  62. }
  63. }
  64. return "";
  65. }
  66.  
  67. static void addMorse(char c){//lower case
  68. if (c == '_'){
  69. morse += "..--";
  70. key[counter] = 4;
  71. counter++;
  72. }
  73. else if (c == ','){
  74. morse += ".-.-";
  75. key[counter] = 4;
  76. counter++;
  77. }
  78. else if (c == '.'){
  79. morse += "---.";
  80. key[counter] = 4;
  81. counter++;
  82. }
  83. else if (c == '?'){
  84. morse += "----";
  85. key[counter] = 4;
  86. counter++;
  87. }
  88. else{
  89. morse += library[(c - 'A')];
  90. key[counter] = library[(c - 'A')].length();
  91. counter++;
  92. }
  93. }
  94. }