简体   繁体   中英

Problems with replacing characters in a string in Java

I want to make a "morse code translator" program, but I have a problem.

I want something like this: when we enter a morse code it should translate it to English and when we enter English text it should return a morse code and I used "Ⓐ" instead of A.

My problem is in translating morse code to English.

When I enter ".--- " it should show "Ⓙ", but it shows "Ⓐ" because the first parts of them are similar.

What should I do to solve this problem?

Here are the parts of the code that I have a problem in:

.replace(".-", "Ⓐ"))
.replace(".---", "Ⓙ"))
.replace("a", ".-  "))
.replace("j", ".---  "))

This is because of:

this one:

.replace(".-", "Ⓐ"))

occurs also here:

.replace(".---", "Ⓙ"))

Consider about 2 strings:

.-.-.-.-.-

.---.---

When you use first replace (ex. replaceAll), then strings will looks like:

ⒶⒶⒶⒶⒶ

Ⓐ--Ⓐ--

Cause .- is inside first and second string.

To avoid that you can use Regex for example

https://www.tutorialspoint.com/java/java_regular_expressions.htm

Or invert the replaces, but this is bad practice (there are so much combinations, that you can't check everything. Good idea is also to split everything into mors code blocks, then check for EQUALITY 1:1 for these strings.

So you will have

1: .-
2: .-
3: .-

1: .---
2: .---
3: .---

Then when you will check for equals:

.- is equal to .-? true, replace

.--- is equal to .-? false, don't replace

Something like this:

public class Main {
    public static void main(String[] args) {
        String mors = ".-- .- .--- .- .---";
        String[] morsBlocks = mors.split(" ");
        String[] morsDecoded = new String[morsBlocks.length];
        decoder(morsBlocks, morsDecoded);
        for(String decoded: morsDecoded) {
            System.out.print(decoded + " ");
        }
        System.out.println("---EOT---");


    }

    public static void decoder(String[] blocks, String[] decoded) {
        for(int index = 0; index < blocks.length; index++) {
            String block = blocks[index];

            switch(block) {
                case ".--":
                    decoded[index] = "bla";
                    break;
                case ".-":
                    decoded[index] = "test";
                    break;
                case ".---":
                    decoded[index] = "omg";
                    break;
                default:
                    decoded[index] = "UNDEFINED";
                    break;
            }

        }
    }
}

Greetings

private static String[][] MORSE_TABLE = {
    {".-", "Ⓐ"},
    {".---", "Ⓙ"},
    {"--...", "7"},
    {"---", "O"},
    {"...", "S"},
    ...
};

The problem is that one should try to convert the longest sequence first, as one otherwise could never translate the longer one.

However that is still ambiguous: do you have A7 or J ? Hence pauses (spaces) are very helpful in those cases.

// Sorting on decreasing morse length:
static {
    Arrays.sort(MORSE_TABLE,
        (pair1, pair2) -> -Integer.compare(pair1[0].length(),
                                          pair2[0].length());
}

And then start at the beginning and do a sequential replace:

List<String> results = decode(s);
if (results.isEmpty()) {
    System.out.println("No results");
} else if (results.size() == 1) {
    System.out.println("Result: " + results.get(0));
} else {
    System.out.println("Ambiguous result: " + results);
}

List<String> decode(String morse) {
    morse = morse.trim();
    if (morse.isEmpty()) {
        List<String> results = new ArrayList<>();
        results.add("");
        return results;
    }
    List<String> results = new ArrayList<>();
    for (String m : MORSE_TABLE) {
        if (morse.startsWith(m[0]) {
            List<String> furthers =
                decode(morse.substring(m[0].length));
            for (String further : furthers) {
                results.add(m[1] + further);
            }
        }
    }
    return results;
}

Ambiguous parsing is possible too, as shown above.

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