I already posted a topic on this but have considerably (and that topic was put on hold..so I can't edit) changed my code (I tried what one of the users said in different variations, but no beans). I've tried running just toMorse, but although it compiles I get no output and an error message of 'java.lang.ArrayIndexOutOfBoundsException(at projmorsejava:22 and 46)'
I'm not sure how to configure toEnglish, at this point I've tried using the replaceAll, indexOf and valueOf methods. I've also tried using plaintextString but that also did not work out (I may have implemented it incorrectly).
Here is my revised code:
import java.util.Scanner;
public class ProjMorse
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] alpha = { "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", "0", " " };
String[] dottie = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|" };
System.out
.println("Enter English to convert from English or Morse to convert from Morse:");
String ans = input.nextLine();
if (ans.equals("English")) {
System.out.println("Please enter the text you would like to convert to Morse Code: ");
String english = input.nextLine();
char[] translates = (english.toLowerCase()).toCharArray();
System.out.println(toMorse(translates, dottie)); //calls on method toMorse
}
else if (ans.equals("Morse")) {
System.out
.println("Please enter the text you would like to convert to English (separate words with '|'):");
String code = input.nextLine();
String[] translates = (code.split("[|]", 0));
System.out.println(toEnglish(translates, alpha));//calls on method toEnglish
}
else
System.out.println("Invalid input, please try again.");
}
public static String toMorse(char [] translates, String [] dottie)
{
String morse = "";
for (int j = 0; j < translates.length; j++)
{
char a = translates[j];
if(Character.isLetter(a))
{
morse = dottie[a + 'a'];
}
}
return morse;/*so I tried running only this(commented other stuff out) and it compiled but although it ran it didnt translate */
}
public static String toEnglish(String [] translates, String [] alpha)
{
String s;
for (int n = 0; n < translates.length; n++)
{
String a = translates[n];
s = java.util.Arrays.asList(alpha).(Character.toChars(a + 'a')[0]);//I'm not sure what to do here..
}
return s;
}
}