hi I am writing a program that translates a string to morse code, but my output is a little strange. can anyone push me in the right direction? heres my code
public class Translate { String sentence; public Translate(String sentence) { this.sentence = sentence; } public String getSentence() { for(int i = 0; i < sentence.length(); i++) { switch(sentence.charAt(i)) { 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; case '0': System.out.print("-----"); break; case '1': System.out.print(".----"); break; case '2': System.out.print("..---"); break; case '3': System.out.print("...--"); break; case '4': System.out.print("....-"); break; case '5': System.out.print("....."); break; case '6': System.out.print("-...."); break; case '7': System.out.print("--..."); break; case '8': System.out.print("---.."); break; case '9': System.out.print("----."); break; case '.': System.out.print(".-.-"); break; case ',': System.out.print("--..--"); break; case '?': System.out.print("..--.."); break; } } return sentence; } }
//This program creates a list of songs for a CD by reading from a file import java.io.*; import java.util.Scanner; public class Morse { public static void main(String [] args) throws IOException { File file = new File("Morse.txt"); Scanner input = new Scanner(file); char[] characters = new char[36]; String[] symbols = new String[36]; for(int i = 0; i < characters.length; i++) { String line = input.nextLine(); characters[i] = line.charAt(0); symbols[i] = line.substring(1); } Scanner keyboard = new Scanner(System.in); String sentence; System.out.println("Enter the sentence that you wish to be decoded: "); sentence = keyboard.nextLine(); Translate morse = new Translate(sentence); char[] c = new char[sentence.length()]; for(int j = 0; j < sentence.length(); j++) { morse.getSentence(); c[j] = sentence.charAt(j); System.out.println("This translates to : " + c); } } }