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; 
}
7
  • It should also be nice to provide matchCase's code Commented Dec 4, 2017 at 16:37
  • You're accessing the elements in the correct way. The problem must be somewhere else. Commented Dec 4, 2017 at 16:38
  • 2
    word == "." ? word.equals(".") rather? Commented Dec 4, 2017 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. Commented Dec 4, 2017 at 16:46
  • Have you tested matchCase separately? It seems to make problems when template is longer than the translated word. Commented Dec 4, 2017 at 17:06

1 Answer 1

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?

...

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

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.