需要补偿摩尔斯码解码器中的空白字符。

huangapple go评论62阅读模式
英文:

need to compensate for whitespace in a morsecode decoder

问题

这是您的代码的翻译部分:

public static String Decode(String morseCode) {
    String MLetter = ""; // Morse数组以容纳单个字符
    String ELetter = ""; // 英文字母字符串
    morseCode += "  "; // 添加两个空格以处理末尾的空白
    for (int i = 0; i < morseCode.length(); i++) // 将字符放入Morse字符串
    {
        if (morseCode.substring(i, i + 1).equals(" ")) // 如果检测到空格
        {
            for (int j = 0; j < morse.length; j++) // 然后将字符串与morse数组进行比较
            {
                if (MLetter.equals(morse[j])) // 如果Morse字符串等于morse索引
                {
                    ELetter += english[j]; // 添加英文等效
                    MLetter = ""; // 并清除Morse字符串
                    i++; // 跳过空格
                }
            }
        }
        MLetter += morseCode.substring(i, i + 1); // 将字符添加到Morse字符串

    }

    return ELetter.toUpperCase();
}

请注意,为了处理输入开头和结尾的空格,我在输入的末尾添加了两个空格,并将其与已有的空格分隔开来。这应该解决了您提到的问题。

英文:

So I made a Morse Code decoder that works pretty well. the only problem is that it doesn't compensate for whitespace at the beginning and end of the entry. So say that I have "." which is E in morse code. I should be able to input "__._" and get the same result, yet I simply get an exception. Ive tried using the trim method on the entry when it comes in, and that gets rid of the exception but I get no output. Ive tried using a loop to get rid of the front whitespace but I get no output, and even if that worked then I would still need to figure out how to handle the whitespace in the back. Here's my code:

 static String[] english = { &quot; &quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;i&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;,
            &quot;m&quot;, &quot;n&quot;, &quot;o&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;u&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, 
            &quot;y&quot;, &quot;z&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;0&quot;,
            &quot;,&quot;, &quot;.&quot;, &quot;?&quot;,&quot;!&quot;,&quot;@&quot;,&quot;&amp;&quot;,&quot;(&quot;,&quot;)&quot;};   //Defining a Character Array of the English Letters numbers and Symbols so that we can compare and convert later 

   static  String[] morse = { &quot; &quot; ,&quot;.-&quot;, &quot;-...&quot;, &quot;-.-.&quot;, &quot;-..&quot;, &quot;.&quot;, &quot;..-.&quot;, &quot;--.&quot;, &quot;....&quot;, &quot;..&quot;, 
               &quot;.---&quot;, &quot;-.-&quot;, &quot;.-..&quot;, &quot;--&quot;, &quot;-.&quot;, &quot;---&quot;, &quot;.--.&quot;, &quot;--.-&quot;, &quot;.-.&quot;,
               &quot;...&quot;, &quot;-&quot;, &quot;..-&quot;, &quot;...-&quot;, &quot;.--&quot;, &quot;-..-&quot;, &quot;-.--&quot;, &quot;--..&quot;, &quot;.----&quot;,
               &quot;..---&quot;, &quot;...--&quot;, &quot;....-&quot;, &quot;.....&quot;, &quot;-....&quot;, &quot;--...&quot;, &quot;---..&quot;, &quot;----.&quot;,
               &quot;-----&quot;, &quot;--..--&quot;, &quot;.-.-.-&quot;, &quot;..--..&quot;,&quot;-.-.--&quot;,&quot;.--.-.&quot;,&quot;.-...&quot;,&quot;-.--.&quot;,&quot;-.--.-&quot;}; 
public static String Decode (String morseCode)
{
	
	String MLetter= &quot;&quot;;//Morsec array to hold individual characters
	String ELetter =&quot;&quot;;//English Letter String
	morseCode+=&quot;  &quot;;
	for(int i=0; i&lt;morseCode.length();i++)//put characters into the morse String
	{
		if(morseCode.substring(i,i+1).equals(&quot; &quot;))//if a space is detected
		{	
			for(int j=0; j&lt;morse.length;j++)// then compare the string to the morse array
				{
					if(MLetter.equals(morse[j]))//if the morse string equals the morse index
						{
							ELetter+=english[j];//add the english equivilent
							MLetter=&quot;&quot;;//and clear the Morse string
							i++;//Skip the space
						}
				}
		}
		MLetter+= morseCode.substring(i,i+1);//add the character to the Morse String
   		
	}

return ELetter.toUpperCase();
}

答案1

得分: 1

丑陋但有效的代码部分:

        String MLetter = ""; // 用于保存单个字符的摩尔斯数组
        String ELetter = ""; // 英文字母字符串
        String morseCodeTrimmed = "";
        // 去除前端的空格
        while (morseCode.startsWith(" ")) {
            morseCode = morseCode.substring(1, morseCode.length());
        }
        // 去除末尾的空格
        while (morseCode.endsWith(" ")) {
            morseCode = morseCode.substring(0, morseCode.length() - 1);
        }
        // 由于某种原因,我不太确定,需要将两个空格重新添加到字符串的末尾。
        // 一个好的解决方案不应该有这个。
        morseCodeTrimmed = morseCode + "  ";

请注意,我只翻译了代码部分,没有包括注释。

英文:

Ugly but works:

        String MLetter= &quot;&quot;;//Morsec array to hold individual characters
        String ELetter =&quot;&quot;;//English Letter String
        String morseCodeTrimmed = &quot;&quot;;
        // trim front white spaces
        while (morseCode.startsWith(&quot; &quot;)) {
            morseCode = morseCode.substring(1, morseCode.length());
        }
        // trim ending white spaces
        while (morseCode.endsWith(&quot; &quot;)) {
            morseCode = morseCode.substring(0, morseCode.length() - 1);
        }
        // for some reason that I&#39;m not quite sure, 2 white spaces need 
        // to be added back to the end of the string.  
        // A good solution will not have this.
        morseCodeTrimmed = morseCode + &quot;  &quot;;

        ... 

huangapple
  • 本文由 发表于 2020年8月9日 01:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63318488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定