0

I am working on a morse-English translator. I have a program (Source Code 2 below) that works, but I am asking extra. When I use a switch construct (source code 1), it doesn't work (doesn't allow user to input English or Morse Code*) but when I use an if-else construct (source code 2), it works**! Why is that?

*Source Code 1 For example, the output would be:

Enter your choice: 
1. 1 for English -> Morse Code 
2. 2 for Morse Code -> English
1
Enter an English sentence.
  • Stops executing here - does not allow user to input. -

**Source Code 2 For example, the output would be:

Enter your choice: 
1. 1 for English -> Morse Code 
2. 2 for Morse Code -> English
1
Enter Morse Code. | for Morse stands for a blank space in English, and a blank space for Morse stands in between each letter/digit in English.
.- -... | -.-
ab c

Source Code 1:

// Import Scanner.
import java.util.Scanner;

public class junk
{

    public static void main(String[] args) {

        // Define arrays.
        char[] english = { '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', ' ' };
        String[] morseCode = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|" };

        // Prompt user for input.
        System.out.println("Enter your choice: \n1. 1 for English -> Morse Code \n2. 2 for Morse Code -> English");
        Scanner input = new Scanner(System.in); // Create new Scanner.
        int inputUser = input.nextInt();

        // Switch statement.
        switch (inputUser)
        {
            case 1:
            {
                // If user enters 1 (English to Morse Code).
                // Prompt user for input.
                System.out.println("Enter an English sentence.");
                String toMorseCode = input.nextLine();
                for (int x = 0; x < toMorseCode.length(); x++)
                {
                    for (int y = 0; y < english.length; y++)
                    {
                        if (english[y] == toMorseCode.charAt(x))
                            System.out.print(morseCode[y] + " ");
                    }
                }
                break;
            }

            case 2:
            {
                // If user enters 2 (Morse Code to English).
                // Prompt user for input.
                System.out.println("Enter Morse Code. | for Morse stands for a blank space in English, and a blank space for Morse stands in between each letter/digit in English.");
                String toEnglish = input.nextLine();

                // Split into toEnglishParts (with space).
                String[] toEnglishParts = toEnglish.split(" ");
                for (int p = 0; p < toEnglishParts.length; p++)
                {
                    for (int y = 0; y < morseCode.length; y++)
                    {
                        // Matches morseCode with toEnglishParts.
                        if (morseCode[y].equals(toEnglishParts[p]))
                                System.out.print(english[y]);
                    }
                }
                break;
            }

            default:
                // If user does not enter 1 or 2.
                System.out.println("Invalid input.")
        }
    }
}

Source Code 2:

// Import Scanner.
import java.util.Scanner;

public class junk
{

    public static void main(String[] args) {

        // Define arrays.
        char[] english = { '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', ' ' };
        String[] morseCode = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|" };

        // Prompt user for input.
        System.out.println("Enter your choice: \n1. A for English -> Morse Code \n2. B for Morse Code -> English");
        Scanner input = new Scanner(System.in); // Create new Scanner.
        String inputUser = input.nextLine();

        // if statement for translating to Morse Code.
        if (inputUser.equalsIgnoreCase("A")) {
            System.out.println("Enter an English sentence.");
            String toMorseCode = input.nextLine();
            for (int x = 0; x < toMorseCode.length(); x++)
            {
                for (int y = 0; y < english.length; y++)
                {
                    if (english[y] == toMorseCode.charAt(x))
                        System.out.print(morseCode[y] + " ");
                }
            }
        }

        // if statement for translating to English.
        else if (inputUser.equalsIgnoreCase("B")) {
            System.out.println("Enter Morse Code. | for Morse stands for a blank space in English, and a blank space for Morse stands in between each letter/digit in English.");
            String toEnglish = input.nextLine();
            String[] toEnglishParts = toEnglish.split(" ");
            for (int p = 0; p < toEnglishParts.length; p++) {
                for (int y = 0; y < morseCode.length; y++) {
                    if (morseCode[y].equals(toEnglishParts[p]))
                        System.out.print(english[y]);
                }    
            }    
        }

        // else - user does not type in A or B.
        else
            System.out.println("Invalid input.");
    }
}
matematika
  • 83
  • 9
  • 1
    What's that 'Z' on Line:27? – progyammer Aug 15 '17 at 18:55
  • 2
    Why do you bother us with Scanner, morse code parser etc. when your question concerns only the difference between switch and if..else? – Heri Aug 15 '17 at 18:56
  • @progyammer Thanks for that I don't know - fixed it (accidents happen)! – matematika Aug 15 '17 at 19:16
  • @Heri I really didn't feel like deleting it (lazy and I have lots to do) - but I'll do it next time :) – matematika Aug 15 '17 at 19:17
  • Such accidents hardly happen if you boil down your code in a way that lets us try out in our own IDE. You should first run it in your IDE (after stripped off to a minimum which shows the problem), and then you can copy/paste it here into the question. But mostly this is no more necessary because you mostly find the problem yourself if you strip off unnecessary code. – Heri Aug 15 '17 at 19:22
  • Your switch-case version will also work; just add an `input.nextLine();` before starting the switch statement to consume the newline character which is causing the trouble in your case... – Am_I_Helpful Aug 15 '17 at 19:25
  • @Heri - so we really needed the Scanner (see top), otherwise question would be different :) – matematika Aug 15 '17 at 19:28

0 Answers0