0

i'm kinda new to Java so and currently studying it at school, but yeah on one of our assignments they asked us to create a program that reads a letter given by a user and then gives the corresponding morse code. They also specified that we have to use 2 arrays, one with the 26 letters of the alphabet and one with their corresponding morse codes for that assignment. Where i'm stuck is I dunno how to make the program look through 2 arrays and make it understand that the first index of the first array equals the first index of the second array, and etc.

Heere's my code so far (and yes its in french because i'm in a french speaking province and school so if you need clarification on my code, just lemme know):

import javax.swing.JOptionPane;

/**
 *
 * @author alex_
 */
public class Morse {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        char lettre = ' ';
        
        char tLettre[] = {'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'};
        String tMorse[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",
               ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
               "..-","...-",".--","-..-","-.--","--.."};
        
        lettre = JOptionPane.showInputDialog("Entrez la lettre que vous voulez traduire: ").charAt(0);
    }
    
}

So yeah sorry for my english if it's not that good

  • [This question](https://stackoverflow.com/questions/29706653/morse-code-translatorsimple) is of a similar vain and should help you along the way! – Robert Wilson Nov 02 '21 at 15:17

1 Answers1

0

Okay so,

You don't actually have to loop through both arrays. Its enough to loop through the first one (containing the letters) like here:

for(int i = 0; i < tLettre.length(); i++){
   // Here the other code
}

Inside there you should compare the given letter (from the user) with the one in your Array at the index i (tLettre[i]). If it is the correct/matching letter then you got the correct index (since both arrays are sorted the same way).

Then you can access the morse String by writing tMorse[i] and print it to the console like System.out.println(tMorse[i]).

Since that's and assignment, i didn't wrote the complete code. I hope that still helps.

Just say if there are any further questions.

have much success and fun

WBoe
  • 46
  • 2
  • 7