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.");
}
}