0

The overall goal of what I'm trying to do is to compare a string to index 0 of an array (that is held within an arraylist), and if the strings are the same (ignoring case), call a method that matches the case of the string to the translated word (held at index 1 of the array inside an arraylist). When I run this code and I print out the contents of my translated arraylist, I get all "no match" characters. I'm assuming this is because I'm not accessing the index I want in the correct manner. Please help!

public static String translate(String word, ArrayList<String[]> wordList) { 
    if (word == "." || word == "!" || word == ";" || word == ":") {
        return word;
    }
    for (int i = 0; i < wordList.size(); i++) {
            String origWord = wordList.get(i)[0];
            String transWord = wordList.get(i)[1];

            if (word.equalsIgnoreCase(origWord)) { //FIXME may need to change if you need to switch from translated to original
                String translated = matchCase(word, transWord);
                return translated;
            }
    }
    String noMatch = Character.toString(Config.LINE_CHAR);     


    return noMatch;
}

Sample Data and expected result

word = "hello"

wordList.get(i)[0] = "Hello"

wordList.get(i)[1] = "Hola"

(word and wordList.get(i)[0] match, so the next step is executed)

match case method is called and returns the translated word with the same case as the original word ->

translated = "hola"

returns the translated word.

(the for loop iterates through the entire wordList until it finds a match, then it calls the translate method)

**

Match Case's Code

 public static String matchCase(String template, String original) {
    String matched = "";
    if (template.length() > original.length()) {

        for (int i = 1; i <= original.length(); i++) {
            if (template.charAt(i-1) >= 'a' &&  template.charAt(i-1) <= 'z') {

                if (i == original.length()) {

                matched +=  original.substring(original.length() - 1).toLowerCase();

                }
                else {

                    matched += original.substring((i-1), i).toLowerCase();
                }

            }
            else if (template.charAt(i-1) >= 'A' && template.charAt(i-1) <= 'Z') {
                if (i == original.length()) {

                    matched += original.substring(original.length() - 1).toUpperCase();

                }
                else {

                    matched +=  original.substring((i-1), i).toUpperCase();

                }
            }
        }
        return matched;
     }
     else if (template.length() < original.length()) {
         int o;
            original.toLowerCase();
            for (int i = 1; i <= template.length(); i++) {
                if (template.charAt(i-1) >= 'a' &&  template.charAt(i-1) <= 'z') {
                    if (i == template.length()) {
                        matched += original.substring(original.length() - 1).toLowerCase();
                    }
                    else {
                        matched += original.substring((i-1), i).toLowerCase();
                    }
                }
                else if (template.charAt(i-1) >= 'A' && template.charAt(i-1) <= 'Z') {
                    if (i == template.length()) {
                        matched += original.substring(original.length() - 1).toUpperCase();
                    }
                    else {
                        matched += original.substring((i-1), i).toUpperCase();
                    }
                }
                String newMatched = matched + original.substring(i, original.length() - 1);
                matched = newMatched;
                newMatched = "";
            }

            return matched;
     }

   return original; 
}
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
punchkey
  • 81
  • 1
  • 11
  • It should also be nice to provide `matchCase`'s code – C.Champagne Dec 04 '17 at 16:37
  • You're accessing the elements in the correct way. The problem must be somewhere else. – QBrute Dec 04 '17 at 16:38
  • 2
    word == "." ? word.equals(".") rather? – Brian Agnew Dec 04 '17 at 16:43
  • Yes there are some remarks we could make such as using `equals` method intead of `==`operator or set `NoMatch` as a constant (`final static`) but it doesn't seem to be the root cause here. – C.Champagne Dec 04 '17 at 16:46
  • Have you tested `matchCase` separately? It seems to make problems when template is longer than the translated word. – C.Champagne Dec 04 '17 at 17:06
  • matchCase is definitely causing me problems, but I think that's a seperate issue, since the translate method only returns the noMatch character if String word doesn't equal any word in the wordList. – punchkey Dec 04 '17 at 17:35
  • Huh? That's exactly what you wrote though, if you don't match any original word at `.get(i)[0]`, then you return `noMatch`. Whereas #matchCase is full of problems, explain the necessity / usage for `#matchCase` in more detail – Rogue Dec 04 '17 at 17:53

1 Answers1

1

I have tested your code and it works rather well with the example you have provided. I cannot help for your bug.

There are however some bugs to notify and improvement to suggest:

  1. matchCase fails when template is shorter than the translated word.
  2. Never compare strings with ==. Use the equals method and look why .
  3. This is not really important but why is noMatch always computed. Why don't you declare it as a constant once?

    public static final String NO_MATCH = String.valueOf(Config.LINE_CHAR);

  4. More importantly I think that matchCase is not really pertinent by design and is over complicated. I think that You should just determine if the word to translate is all lower case or upper case or with the first letter in uppercase and the following letters in lower case. What you do (comparing the case letter by letter) is not really pertinent when the length is different.

  5. When you consider a single character, use charAt instead of substringit is simpler and faster.
  6. You also might have a look a regex to analyze your Strings.
  7. Have you considered Maps for your translation lookup?

...

C.Champagne
  • 5,381
  • 2
  • 23
  • 35