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++;
}`
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.StringAPI offers a toCharArray() method. Consider simplifying your code by usingindexmethods from theStringAPI , 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 .