1

I've created the following:

import java.util.*;

public class morse5 {

public static void main(String [] args)
{     

  //Declare Variables
  String [][] myIndexAlphaMorse = {{"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", "-----"}, {" ", "|"}}; 


  //Test
  System.out.println(Arrays.deepToString(myIndexAlphaMorse));
  System.out.println(myIndexAlphaMorse[8][0]);


}  

What I would like to know is how to get the value of the corresponding position based on user input. I'm learning, so I just want the piece on how to get, as an example, .- back when "a" is entered.

5 Answers 5

3

Simply iterate over you array and compare the 0th element at each position with the character you are looking for.

String input = "v";
String result= "";
for(int i = 0; i < myIndexAlphaMorse.length; i++){
    if(myIndexAlphaMorse[i][0].equals(input)){
        result = myIndexAlphaMorse[i][1];
        break;
   }
}
System.out.println("morse for " + input + " = " + result);

But as the other answer says you should use a map that whould fit perfect for this task.

Sign up to request clarification or add additional context in comments.

2 Comments

This is probably closer to the intent. I've learned for loops. I'll give this a try.
This works! Thank you all for your help and quick responses. I'm sure all of the answers were correct; I just needed the one at my low level!
2

You should consider using a Map instead of a two dimensional array for this problem:

Map<String, String> myIndexAlphaMap = new HashMap<>();
myIndexAlphaMap.put("a", ".-");
myIndexAlphaMap.put("b", "-...");
// etc.

// given user input of "a" you can access via
myIndexAlphaMap.get("a");

Comments

1

Or a map of string arrays

Map<String, String[]> myIndexAlphaMap = new HashMap<String, String[]>();
myIndexAlphaMap.put("a", new String {".","-"});
myIndexAlphaMap.put("b", new String {"-",".","."});

// given user input of "a" you can access via
myIndexAlphaMap.get("a")[0];

1 Comment

I haven't learned maps, yet. I think the point of this exercise is for me to learn how to take an entered value and, using either a 1D or 2D array, return the corresponding value.
1

You can also use Hash tables, since they've already given sample above of HashMap/Map:

Hashtable<String, String> table = new Hashtable<String, String>();
table.put("a", ".-");
table.put("b", "-...");

It is also synchronized and thread safe unlike HashMaps, albeit is a smidgen slower for bigger data sets.

Comments

1

First of all you have to read the letter as the String object. Then you can just iterate through your array and when we find what we are looking for - just print it and break the loop:

Scanner scanner = new Scanner(new InputStreamReader(System.in));
String letter = scanner.next();
for (int i=0; i<myIndexAlphaMorse.length; i++)
    if (myIndexAlphaMorse[i][0].equals(letter)){
        System.out.println(myIndexAlphaMorse[i][1]);
        break;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.