0

This program is using string methods to convert from English to morse code. when executing the code I seem to be stuck in an input mode before the [int KeyIndex = 0;] line. I am only testing for the English to morse code loop, so I have not included the morse to english portion of the code.

`public static void main(String[] args) { Scanner input = new Scanner(System.in);

  char[] EngMorseArray =
  { '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' };

  String[] MorseEngArray =
  { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
        "--.-", ".-.", "…", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--",
        "....-", ".....", "-....", "--...", "---..", "----." };

  // First we identify if we are going eng - morse or vice versa
  System.out.println("for Eng -> Morse, enter 1.  for Morse -> Eng, enter 2");
  String WhichConversion = input.next();

  if (WhichConversion.equals("1")) // English to Morse
  {
     System.out.println("What is the text you would like coded?"
           + " (lowercase a-z and 0-9 with a space between words, " + "please) ");
     
     String CharString = input.nextLine(); // this line is here twice
     CharString = input.nextLine(); // to avoid nextInt/nextLine issue
     
     char[] CharStringArray = new char[CharString.length() + 1];
     
     
     for (int index = 0; index < CharString.length(); index++) // Convert string to char array
     {
        CharStringArray[index] = CharString.charAt(index);
        System.out.print(CharStringArray[index]);
     }
     /*
      * convert from char Array to string again for (int index = 0; index <=
      * CharStringArray[index]; index++) { String charString = ""; charString +=
      * CharStringArray[index]; }
      */
     
     int KeyIndex = 0;
     int InputIndex;

     for (InputIndex = 0; InputIndex < EngMorseArray.length; InputIndex++)
     {

        while (KeyIndex <= EngMorseArray.length)
        {
           
           if (EngMorseArray[KeyIndex] == CharStringArray[InputIndex])
           {
              System.out.println(MorseEngArray[KeyIndex] + " ");
              KeyIndex++;
           } else if (CharStringArray[InputIndex] == ' ')
           {
              System.out.println(" | ");
              KeyIndex++;
           }`
7
  • 1
    First of all, I advise you to create and use relevant methods to your code, this is very hard to read like that and not easy to tell what's going on Commented Oct 5, 2023 at 15:52
  • 1
    You'll probably encounter an endless loop in while (KeyIndex <= EngMorseArray.length), because if neither your if nor the else is entered you just do nothing and the loop gets stuck because the variables you use to check its condition never change. Commented Oct 5, 2023 at 15:57
  • Have you run this code in a debugger? If you haven't, you should. Set a breakpoint prior to where you think the problem is, and then run the code one-step-at-a-time while monitoring variables. Commented Oct 5, 2023 at 16:32
  • My first choice for debugging is placing ubiquitous print statements at key locations. Print values to ensure they are what you expect them to be, accompanied by some indication to uniquely identify each print statement. And if you don't know how to use a debugger, practice on working code to get familiar with it prior to really needing it. Commented Oct 5, 2023 at 16:49
  • Off-topic: The Java String API offers a toCharArray() method. Consider simplifying your code by using index methods from the String API , and / or binary search methods of the Arrays API . Also, your code will be easier for others to read if you follow Java naming conventions . Commented Oct 5, 2023 at 16:49

4 Answers 4

1

Your problem seems to be in the nested loops.

  1. Your loop condition should be CharStringArray.length instead of EngMorseArray.length in the outer loop.
  2. Your KeyIndex should be initialized inside the outer loop.
  3. KeyIndex should incremented when no match is found.
  4. When you find a match, you should break out of inner loop.

Here is the updated code snippet.

            /*
             * convert from char Array to string again for (int index = 0; index <=
             * CharStringArray[index]; index++) { String charString = ""; charString +=
             * CharStringArray[index]; }
             */
            System.out.println();
            int InputIndex;
            for (InputIndex = 0; InputIndex < CharStringArray.length; InputIndex++) {
                if (CharStringArray[InputIndex] == ' ') {
                    System.out.print(" | ");
                    continue;
                }

                int KeyIndex = 0;
                while (KeyIndex < EngMorseArray.length) {
                    if (EngMorseArray[KeyIndex] == CharStringArray[InputIndex]) {
                        System.out.print(MorseEngArray[KeyIndex] + " ");
                        break;
                    }
                    KeyIndex++;
                }
            }

There are other places where the code can be improved. Like using library method String.toCharArray() to convert string to char array. You can use maps instead of arrays to keep key, value pair.

Sign up to request clarification or add additional context in comments.

Comments

0

Ok so here is the new and improved program based on everyone's input. First, the main program:

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

String WhichConversion; // 1 means EngToMorse, 2 means MorseToEng

String EngInput; // raw string input for EngToMorse
String CodeOutput; // Output from EngToMorse
char[] EngInputArray; // EngInput converted to char array

String CodeInput; // raw code input for MorseToEng
String EngOutput; // Output from MorseToEng

// First we identify if we are going eng - morse or vice versa
System.out.println("for Eng -> Morse, enter 1.  for Morse -> Eng, enter 2");
WhichConversion = input.next();

// English to Morse
if (WhichConversion.equals("1"))
{
   System.out.println("What is the text you would like coded?"
   + " (lowercase a-z and 0-9 with a space between words, " + "please) 
   ");

EngInput = input.nextLine(); // this line is here twice
EngInput = input.nextLine(); // to avoid nextInt/nextLine issue

// declare array after EngInput is defined
EngInputArray = new char[EngInput.length() + 1];

// Convert EngInput to EngInputArray
for (int index = 0; index < EngInput.length(); index++)
{
    EngInputArray[index] += EngInput.charAt(index);
}
System.out.print(EngInputArray);
System.out.println(" ");

convertToMorse(EngInputArray);
}
 '''

And here is the method for the conversion from English to morse code:

private static void convertToMorse(char[] EngInputArray)
{
    char[] EngArray =
    { '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' };

    String[] MorseArray =
    { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", 
        "-.- ", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "…", "-", "..- 
        ", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", 
        "...--", "....-", ".....", "-....", "--...", "---..", "----." 
    };

int KeyIndex = 0;
int InputIndex = 0;

for (InputIndex = 0; InputIndex < EngArray.length; InputIndex++)
{
    KeyIndex = 0;
    System.out.print(" ");

    while (KeyIndex < EngArray.length & InputIndex < EngInputArray.length)
    {
        if (EngArray[KeyIndex] == EngInputArray[InputIndex])
        {
             System.out.print(MorseArray[KeyIndex] + " ");
             KeyIndex = EngArray.length;
             break;
        } else if (EngInputArray[InputIndex] == ' ')
        {
            System.out.print(" | ");
            KeyIndex = EngArray.length;
            break;
        } else if (EngInputArray[InputIndex] != ' ' & EngArray[KeyIndex] != 
            EngInputArray[InputIndex])
        {
           KeyIndex++;
        }
       }
    }
} // End EngToMorse

1 Comment

By the way, instead of EngInput = input.nextLine(); to read the end of the preceding line, you could have input.nextLine(); // read and discard remaining characters in input buffer. Even though input.nextLine() returns a value, you are not required to "see" the result. input.nextLine() is being called for its side effect.
0

"... when executing the code I seem to be stuck in an input mode before the [int KeyIndex = 0;] line. ..."

Use the next method if you're parsing more than one value from the input line.
Othewise, use the nextLine method.

String WhichConversion = input.nextLine();

Then, the CharString assignment will only require one nextLine call.

Comments

0

I'd like to illustrate how using arithmetic on char types can simplify this code. It's not really related to the question about input.

First, the Morse code array should be static data of the class. After all, the same data will be needed to encode to Morse as decode from Morse.

public class Puppy_with_pinecone {
    
    private static final String[] MORSE_CODE =  { 
          // letters a to z (lowercase)
          "•‒", "‒•••", "‒•‒•", "‒••", "•" , "••‒•", "‒‒•", "••••", "••"
        , "•‒‒‒", "‒•‒ ", "•‒••", "‒‒", "‒•", "‒‒‒", "•‒‒•", "‒‒•‒", "•‒•"
        , "•••", "‒", "••‒", "•••‒", "•‒‒", "‒••‒", "‒•‒‒", "‒‒••"
         // digits 0 to 9      
        , "‒‒‒‒‒", "•‒‒‒‒", "••‒‒‒", "•••‒‒", "••••‒" 
        , "•••••", "‒••••", "‒‒•••", "‒‒‒••", "‒‒‒‒•"    
    };
    
    private static final char SPACE = ' ';

    public static void main(String[] args) {
    ...
}

The following works because the letters A to Z are consecutive in ASCII, as are the letters a to z and the digits 0 to 9.[1] And Unicode is based on ASCII.

The char type in Java is an integer type primitive. So, char data can be used for arithmetic.

    
    public static String toMorse (String input) {

        StringBuilder output = new StringBuilder(input.length() * 6);           
            
        for (int inpIndex = 0; inpIndex < input.length(); inpIndex++) {
            char letter = Character.toLowerCase(input.charAt(inpIndex));
            if (letter >= 'a' && letter <= 'z') {
                output.append (MORSE_CODE [letter - 'a']).append(SPACE);
            } else if (letter >= '0' && letter <= '9') {
                output.append (MORSE_CODE [letter - '0' + 26]).append (SPACE);
            }
            else if (Character.isWhitespace(letter))  {
                output.append (" | ");
            }
        }
        return output.toString();
    }  

  • Returning a String is in keeping with separation of responsibilities design. Having the output handled outside the method that does the conversion makes it easier to reuse the conversion method. You might, for example, want to write the Morse code to a file, or pass it to a method that generates the sound of Morse code.

[1] Note this is not true of EBCDIC.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.