This Java code should prompt the user to choose between Morse code and English, then prompt them to enter a string in the language they chose. It should then produce a translation to the other language. It compiles on my machine, though it sometimes doesn't work correctly.
Can someone else try and run it for me and just tell me if it works? If not, can you point me to the error in my code that is producing the runtime error?
public class MorseCodeJavaProgram
{
public static void morse( String s3 )
{
int letters [ ] = new int [ 26 ];
for ( int num = 0; num < s3.length(); num++ )
{
switch ( s3.charAt( num ) )
{
case 'a':
System.out.print( ".- ");
break;
case 'b':
System.out.print( "-… ");
break;
case 'c':
System.out.print( "-.-. ");
break;
case 'd':
System.out.print( "-.. ");
break;
case 'e':
System.out.print( ". ");
break;
case 'f':
System.out.print( "..-. ");
break;
case 'g':
System.out.print( "--. ");
break;
case 'h':
System.out.print( "…. ");
break;
case 'i':
System.out.print( ".. ");
break;
case 'j':
System.out.print( ".--- ");
break;
case 'k':
System.out.print( "-.- ");
break;
case 'l':
System.out.print( ".-.. ");
break;
case 'm':
System.out.print( "-- ");
break;
case 'n':
System.out.print( "-. ");
break;
case 'o':
System.out.print( "--- ");
break;
case 'p':
System.out.print( ".--. ");
break;
case 'q':
System.out.print( "--.- ");
break;
case 'r':
System.out.print( ".-. ");
break;
case 's':
System.out.print( "... ");
break;
case 't':
System.out.print( "- ");
break;
case 'u':
System.out.print( "..- ");
break;
case 'v':
System.out.print( "...- ");
break;
case 'w':
System.out.print( ".-- ");
break;
case 'x':
System.out.print( "-..- ");
break;
case 'y':
System.out.print( "-.-- ");
break;
case 'z':
System.out.print( "--.. ");
break;
case ' ':
System.out.print( " | ");
break;
}
}
}
public static void toEnglish( String s1 )
{
String english [ ] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", " " };
String morse [ ] = { ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", "…. ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "| " };
for ( int num = 0; num < s1.length(); num++ )
{
if ( s1.charAt ( num ) == ' ')
{
for ( int num2 = num; num2 < s1.length(); num2++ )
{
if ( s1.charAt ( num2++ ) == ' ')
{
for ( int num3 = 0; num < 26; num3++ )
{
if ( s1.substring( num++, num2 + 2 ) == ( morse [ num3 ] ))
{
System.out.print( english [ num3 ] );
}
}
}
}
}
}
}
public static void main( String [] args)
{
System.out.println("Begin Program");
String s2 = Input.getString( "To Morse or From Morse" );
if ("From Morse".equals(s2) ){
String s1 = Input.getString( "Please type a phrase in English" );
toEnglish( " " + s1 + " " );
}
if ("To Morse".equals(s2) )
{
String s3 = Input.getString( "Please type a phrase in Morse Code" );
morse( s3 );
}
}
}
When I get an error it states "Check console for possible error message, unable to be launched."
Below I added the Input.java file to be compiled to Input.class
import javax.swing.*;
public class Input
{
public static byte getByte( String s )
{
String input = JOptionPane.showInputDialog( s );
return Byte.parseByte( input );
}
public static short getShort( String s )
{
String input = JOptionPane.showInputDialog( s );
return Short.parseShort( input );
}
public static int getInt( String s )
{
String input = JOptionPane.showInputDialog( s );
return Integer.parseInt( input );
}
public static long getLong( String s )
{
String input = JOptionPane.showInputDialog( s );
return Long.parseLong( input );
}
public static float getFloat( String s )
{
String input = JOptionPane.showInputDialog( s );
return Float.parseFloat( input );
}
public static double getDouble( String s )
{
String input = JOptionPane.showInputDialog( s );
return Double.parseDouble( input );
}
public static boolean getBoolean( String s )
{
String input = JOptionPane.showInputDialog( s );
return Boolean.parseBoolean( input );
}
public static char getChar( String s )
{
String input = JOptionPane.showInputDialog( s );
return input.charAt(0);
}
public static String getString( String s )
{
String input = JOptionPane.showInputDialog( s );
return input;
}
}