Hi
I'm trying to do a Morse to PT translator and vice versa. I want the program to know if the first character of my input is - or. it automatically assumes that I want to translate from Morse to PT and does the translation soon. If the first character is not one of those then it assumes that I want to translate from PT to Morse and translates soon.
Unrecognized symbols are translated to '?'
Another important point is if my input is '*' then the program closes
I am no longer able to make that association of - and. and I think I have the '?' evil
Here's what I have:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char [] Alfabeto = { '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 [] Morse = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
Scanner key = new Scanner (System.in);
System.out.println( "Introduza 'Texto' se quiser traduzir de alfabeto para Morse ou introduza 'Morse' se pretender traduzir de Morse para alfabeto" );
String Resposta = key.nextLine();
char first = Resposta.charAt(0);
if ( first.equals('.') || first.equals('-'))
{
System.out.println("Pedido de SOS");
String texto = key.nextLine();
String[] palavras = null;
if(texto.contains(" ")){
palavras = texto.split(" ");
}
else{
palavras = new String[1];
palavras[0] = texto;
}
for (String letra: palavras )
{
String[] caracteres = letra.split(" ");
for(int h = 0;h < caracteres.length;h++)
{
for(int i = 0;i < Morse.length;i++){
if(caracteres[h].equals(Morse[i])){
System.out.print(Alfabeto[i]);
}
}
}
System.out.print(" ");
}
}
else if ( Resposta.equals("Texto"))
{
System.out.println("Pedido de SOS");
String texto2 = key.nextLine();
texto2 = texto2.toLowerCase ();
for ( int x = 0; x < texto2.length(); x++ )
{
for ( int y = 0; y < Alfabeto.length; y++ )
{
if ( Alfabeto [ y ] == texto2.charAt ( x ) )
System.out.print ( Morse [ y ] + " " );
}
}
}
else if (Resposta.equals("*")) {
System.exit(0);
}
else
{
System.out.println ( "?" );
}
}
}
Thanks for any help!