Morse to Portuguese translator and vice versa

Asked

Viewed 434 times

0

Hi

I’m trying to make a translator from Morse to PT 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 soon makes the translation. If the first character is not one of those then assume that I want to translate from PT to Morse and translate soon.

Unrecognised symbols are translated into '?'

Another important point is if my input is '*' then the program closes

For now I’m not getting that - and . association and I think I have the wrong '?' association

Here’s what I got:

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 ( "?" );

                }
        }
    }

I appreciate any kind of help!

  • Related: https://answall.com/q/255017/28595

  • With the exception of comparison if ( first.equals('.') || first.equals('-')) which must be done with == the program is working. What is the problem exactly ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.