简体   繁体   中英

Checking a String against a hashmap

I want to build a program that translates English to Morse code and visa versa, I have decided to use hash maps to do this, but I'm unsure as to how I could run the string through the hash map and get the translation out at the end. Here is my code at the moment:

import java.util.HashMap;
import java.util.Map;

public class MorseCodeTranslator{

public static String translateToMorseCode() {
    String englishtoMorse = "";
    String translation = null;

    Map<Character, String> morse = new HashMap<Character, String>();
    morse.put('a', "._");
    morse.put('b', "_...");
    morse.put('c',  "_._");
    morse.put('d',  "_..");
    morse.put('e',    ".");
    morse.put('f', ".._.");
    morse.put('g',  "__.");
    morse.put('h', "....");
    morse.put('i',   "..");
    morse.put('j', ".___");
    morse.put('k',   "_.");
    morse.put('l', "._..");
    morse.put('m',   "__");
    morse.put('n',   "_.");
    morse.put('o',  "___");
    morse.put('p', ".__.");
    morse.put('q', "__._");
    morse.put('r', "._.");
    morse.put('s',  "...");
    morse.put('t',   "_");
    morse.put('u',  ".._");
    morse.put('v', "..._");
    morse.put('w',  ".__");
    morse.put('x', "_.._");
    morse.put('y', "_.__");
    morse.put('z', "__..");

    return translation;
}    

public static String translateFromMorseCode() {
    String morsetoEnglish = "";
    String translation = null;

    Map<Character, String> morse = new HashMap<Character, String>();
    morse.put('a', "._");
    morse.put('b', "_...");
    morse.put('c',  "_._");
    morse.put('d',  "_..");
    morse.put('e',    ".");
    morse.put('f', ".._.");
    morse.put('g',  "__.");
    morse.put('h', "....");
    morse.put('i',   "..");
    morse.put('j', ".___");
    morse.put('k',   "_.");
    morse.put('l', "._..");
    morse.put('m',   "__");
    morse.put('n',   "_.");
    morse.put('o',  "___");
    morse.put('p', ".__.");
    morse.put('q', "__._");
    morse.put('r', "._.");
    morse.put('s',  "...");
    morse.put('t',   "_");
    morse.put('u',  ".._");
    morse.put('v', "..._");
    morse.put('w',  ".__");
    morse.put('x', "_.._");
    morse.put('y', "_.__");
    morse.put('z', "__..");  

    return translation;
}
}  

I want to be able to run whatever is in englishtoMorse or morsetoEnglish through the hash map and convert the characters to the value they are associated with in the hash map then output them in translation .

Create the map as static field. Add the morse alphabet. Then create a method which gets as parameter the text to translate. Then iterate the text to translate every char and create with the translated chars the string which will be returned.

I have made an example with your code and my explanations. The code works only for english text in morse. You must add the other direction.

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{

    private static final Map<Character, String> morse = new HashMap<Character, String>();
    static {
    morse.put('a', "._");
    morse.put('b', "_...");
    morse.put('c',  "_._");
    morse.put('d',  "_..");
    morse.put('e',    ".");
    morse.put('f', ".._.");
    morse.put('g',  "__.");
    morse.put('h', "....");
    morse.put('i',   "..");
    morse.put('j', ".___");
    morse.put('k',   "_.");
    morse.put('l', "._..");
    morse.put('m',   "__");
    morse.put('n',   "_.");
    morse.put('o',  "___");
    morse.put('p', ".__.");
    morse.put('q', "__._");
    morse.put('r', "._.");
    morse.put('s',  "...");
    morse.put('t',   "_");
    morse.put('u',  ".._");
    morse.put('v', "..._");
    morse.put('w',  ".__");
    morse.put('x', "_.._");
    morse.put('y', "_.__");
    morse.put('z', "__..");
    morse.put(' ', " ");
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        String str = "Hello World";
        System.out.println(translate(str));
    }



    public static String translate(String text) {
        StringBuilder builder = new StringBuilder();
        String lower = text.toLowerCase();
        for (int i = 0; i < text.length(); i++) {
            builder.append(morse.get(lower.charAt(i)));
        }
        return builder.toString();
    }    

}

Output:

......_..._..___ ._____._.._.._..

Working Example:

http://ideone.com/uWGAtU

As for the "English to Morse" use case, use a for loop like this:

StringBuilder sb = new StringBuilder();
for ( int i = 0; i < englishtoMorse.length(); i++) {
    char c = englishtoMorse.charAt(i);
    sb.append(morse.get(c));
}
translate = sb.toString();

As for the "Morse to English" use case, you need some sort of separator between the morse characters. Otherwise you will end up with a translation consisting only of 'e' and 't'. Let's suppose, your morseToEnglish String uses spaces as separator in between the characters. Then you can do the translation like this:

StringTokenizer st = new StringTokenizer(morseToEnglish);
StringBuilder result = new StringBuilder();
while( st.hasMoreTokens()) {
    result.append(morse.findKey(st.nextToken()));
}
translation = sb.toString();

I hope this helped.

You should initialize the map once (eg, in a static data member, like Zelldon suggested). Once you have done that, a string can be translated from English to morse quite elegantly by using Java 8's streaming APIs:

public static String translateToMorseCode(String english) {
    return english.chars()
                  .boxed()
                  .map(c -> morse.get((char) c.intValue()))
                  .collect(Collectors.joining());
}

EDIT:
As JB Nizet commented, using mapToObj would be more elegant (and efficient, presumably):

public static String translateToMorseCode(String english) {
    return english.chars()
                  .mapToObj(c -> morse.get((char) c))
                  .collect(Collectors.joining());
}

You may use a BiMap here. I think it is more suitable for your case.

BiMap<Character, String> biMap = HashBiMap.create();

biMap.put('a', "._");
biMap.put('b', "_..");

System.out.println("morse code = " + biMap.get('a'));
System.out.println("alphabet = " + biMap.inverse().get("._"));

You may use guava .Visit the link

You can use HashMap as you wish:

    import java.util.HashMap;
    import java.util.Map;

    public class MorseCodeTranslator{
    public static Map<Character, String> morse = new HashMap<Character, String>();
    public static Map<String, Character> english = new HashMap<>();
    public MorseCodeTranslator(){
    morse.put('a', "._");
    morse.put('b', "_...");
    morse.put('c',  "_._");
    morse.put('d',  "_..");
    morse.put('e',    ".");
    morse.put('f', ".._.");
    morse.put('g',  "__.");
    morse.put('h', "....");
    morse.put('i',   "..");
    morse.put('j', ".___");
    morse.put('k',   "_.");
    morse.put('l', "._..");
    morse.put('m',   "__");
    morse.put('n',   "_.");
    morse.put('o',  "___");
    morse.put('p', ".__.");
    morse.put('q', "__._");
    morse.put('r', "._.");
    morse.put('s',  "...");
    morse.put('t',   "_");
    morse.put('u',  ".._");
    morse.put('v', "..._");
    morse.put('w',  ".__");
    morse.put('x', "_.._");
    morse.put('y', "_.__");
    morse.put('z', "__..");
    morse.forEach( (c,s) -> english.put(s, c));
    }


    public static String translateToMorseCode(final String english) {
    char[] data = english.toCharArray();
    StringBuilder result = new StringBuilder();
    for(char c: data) result.append(morse.get(new Character(c)));
    return result.toString();
    }

    public static String translateFromMorseCode(final String morseCode) {
    String[] data = morseCode.split(" ");
    StringBuilder result = new StringBuilder();
    for(String s:data) result.append(english.get(s));
    return result.toString();
    }
    }

Use one table and do a backward search with Map.entrySet(). And don't forget a separator token.

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;

public class MorseCodeTranslator{
    public static Map<Character, String> getMorseTable(){
        Map<Character, String> morse = new HashMap<Character, String>();
        morse.put('a', "._");
        morse.put('b', "_...");
        morse.put('c',  "_._");
        morse.put('d',  "_..");
        morse.put('e',    ".");
        morse.put('f', ".._.");
        morse.put('g',  "__.");
        morse.put('h', "....");
        morse.put('i',   "..");
        morse.put('j', ".___");
        morse.put('k',   "_.");
        morse.put('l', "._..");
        morse.put('m',   "__");
        morse.put('n',   "_.");
        morse.put('o',  "___");
        morse.put('p', ".__.");
        morse.put('q', "__._");
        morse.put('r', "._.");
        morse.put('s',  "...");
        morse.put('t',   "_");
        morse.put('u',  ".._");
        morse.put('v', "..._");
        morse.put('w',  ".__");
        morse.put('x', "_.._");
        morse.put('y', "_.__");
        morse.put('z', "__..");
        return morse;
    }
    public static String toMorse(String text){      
        Map<Character, String> table  = getMorseTable();
        StringBuilder result = new StringBuilder();
        // get every char in text
        for(int i = 0; i < text.length(); i++){
            char c = text.charAt(i);
            // and add morse character from table
            result.append(table.get(c));
            // put character separator
            result.append("|");
        }
        // delete last character separator
        result.deleteCharAt(result.length() - 1);
        return result.toString();
    }

    public static String fromMorse(String morse){
        Map<Character, String> table  = getMorseTable();
        // use string tokenizer to separate morse characters
        StringTokenizer st = new StringTokenizer(morse,"|");
        StringBuilder result = new StringBuilder();
        // get every morse character
        while( st.hasMoreTokens()) {
            String morseChar = st.nextToken();
            // and find equivalent letter in morse table
            for (Entry<Character, String> entry : table.entrySet()) {
                if (entry.getValue().equals(morseChar)) {
                   result.append(entry.getKey());
                   break;
                }
            }
        }
        return result.toString();
    }

    public static void main(String[] args){
        String text = "abcxyz";
        System.out.println(text);
        String morse = toMorse(text);
        System.out.println(morse);
        String back = fromMorse(morse);
        System.out.println(back);
    }
}

Output:

abcxyz
._|_...|_._|_.._|_.__|__..
abcxyz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM