0

I am making a translator that takes english and translates it into a made up language called Talhamr. To do this I take user input and make it into an array. Then I test it against a word bank array that contains english words with the corresponding Talhamr word next to it. If the english word works I want to replace it with the corresponding Talhamr word. Can any one help me code this? bellow is the code I have so far.

Main class:

import java.util.Scanner;

public class Translator {
    public static String userInput = "";

    public static String[] wordBank = {"a/an","Ai","about","circa" /*...*/};

    public static void main(String[] args) {
        /**
         * getting input
         */
        Scanner scanner = new Scanner(System.in);
        System.out.print("Write something to be translated into ancient language: ");
        /**
         * formatting input
         */
        userInput.toLowerCase();
        userInput = scanner.nextLine();
        String[] wordArray = userInput.split(" ");
        /**
         * Creating a, and preforming english to talhammir, translation
         */
        EtoT e2t = new EtoT();
        e2t.translateToTalhamr(wordArray, wordBank);
        System.out.println(wordArray[0]);
        /**
         * This is how I formatted the word bank. After I went to quizlet and changed all the spaces between rows and columns to "-"
         * String makeNice = ...;
         * System.out.println("\""+ makeNice.replace("-", "\",\""));
         */

    }//end of main method   
}//end of Translator class

Second class containing translation method:

public class EtoT {
    /**
     * establishing word bank to loop through and translate
     */
    public String[] wordBank = {"a/an","Ai","about","circa" /*...*/};

    public EtoT() {
    }

    public String translateToTalhamr(String[] wordArray, String[] wordBank) {
        for (int i=0; i < wordArray.length; i++) {
            System.out.print("'");
            for (int j = 0; j < wordBank.length; j++) {
                System.out.print(".");
                if (wordBank[j] == wordArray[i]) {
                    System.out.print(",");
                    wordArray[i] = wordBank[j + 1];
                } else {
                    wordArray[i] = wordArray[i] + "!";
                }
            }//end of inner word bank
        }//end of outer for loop
        System.out.println("\n" + wordArray.toString());
        return wordArray.toString();
    }//end of translateToTalhamr

    public static void main(String[] args) {
    }//end of main method
}//end of EtoT
Tom
  • 16,842
  • 17
  • 45
  • 54
  • 1
    What is your actual problem / question? – tim4242 Feb 20 '16 at 20:21
  • Please read [ask] and make a [mcve]. – Tunaki Feb 20 '16 at 20:21
  • Please mind that I truncated your word database to two example entry, since the whole database only bloated the question. If someone is interested in *Talhamr*, then he can check the version history – Tom Feb 20 '16 at 20:22
  • The above mentioned duplicate explains why `if (wordBank[j] == wordArray[i])` won't work for you. – Tom Feb 20 '16 at 20:25

1 Answers1

0

Your problem is that you are comparing Strings using ==. Because Strings are objects, that checks that they are the same object instead of if they have the same text. To correctly test this, you can use string1.equals(string2)

Specifically for you, in your translateToTalhamr function, you should have wordBank[j].equals(wordArray[i]) instead of wordBank[j] == wordArray[i].

gbear605
  • 89
  • 1
  • 10