4

So I'm trying to write a program that takes a String (str) and converts it to NATO Phonetic Alphabet (newSentence).

This is what I tried:

newSentence = str.toLowerCase().replace("a", "Alpha ")
                               .replace("b", "Bravo ")
                               .replace("c", "Charlie ")
                               .replace("d", "Delta ")
                               .replace("e", "Echo ")
                               .replace("f", "Foxtrot ")
                               .replace("g", "Golf ")
                               .replace("h", "Hotel ")
                               .replace("i", "India ")
                               .replace("j", "Juliet ")
                               .replace("k", "Kilo ")
                               .replace("l", "Lima ")
                               .replace("m", "Mike ")
                               .replace("n", "November ")
                               .replace("o", "Oscar ")
                               .replace("p", "Papa ")
                               .replace("q", "Quebec ")
                               .replace("r", "Romeo ")
                               .replace("s", "Sierra ")
                               .replace("t", "Tango ")
                               .replace("u", "Uniform ")
                               .replace("v", "Victor ")
                               .replace("w", "Whiskey ")
                               .replace("x", "X-Ray ")
                               .replace("y", "Yankee ")
                               .replace("z", "Zulu ");

However, this obviously doesn't work since after it replaces every "a" with "Alpha," it'll take every "l" "p" and "h" and reconvert those and so on. Is there some way to convert all of the letters at once so that this doesn't happen, and make it much more efficient?

2
  • 1
    maybe instead of calling replace a bunch of times, you can iterate over each character and build a completely new string based upon your input string Commented Nov 20, 2013 at 23:29
  • stackoverflow.com/q/7658568/1725096 Commented Nov 20, 2013 at 23:42

3 Answers 3

2

Create a new string iteratively:

StringBuilder outSB = new StringBuilder();
for(int i=0; i < str.length(); i++){
    if(str.charAt(i) == 'a' || str.charAt(i) == 'A') outSB.append("Alpha ");
    if(str.charAt(i) == 'b' || str.charAt(i) == 'B') outSB.append("Bravo ");
    // and so on
}
String output = outSB.toString();
Sign up to request clarification or add additional context in comments.

3 Comments

aren't you supposed to pass i into charAt?
@SamIam My oversight, fixed.
Awesome, thanks, worked perfectly. The only thing I changed is I turned every if besides the first one to else if and at the end I added else outSB.append(str.charAt(i) + " "); for if it wasn't a letter.
1
private static Map<Character, String> lookup = new HashMap<>();
public static void main(String[] args) {
    String input = "abc";
    StringBuilder sb = new StringBuilder();

    for(char c : input.toCharArray()){
        sb.append(lookup.get(c));
        sb.append(" ");
    }

    System.out.println(sb.toString());

}

static {
    lookup.put('a', "Alpha");
    lookup.put('b', "Bravo");
    lookup.put('c', "Charlie");
}

Output:

Alpha Bravo Charlie

Create a lookup table and construct a new string based on each character in your original string.

Comments

0
StringBuilder sb = new StringBuilder();
for (char c : str.toLowerCase().toCharArray()) {
    switch (c) {
        case 'a':  
            sb.append("Alpha ");
            break;
        case 'b':  
            sb.append("Bravo ");
            break;
        // and so on until Zulu...
        default:
            // optional: just pass through any character not in NATO alphabet
            sb.append(c);
            break;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.