首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java翻译错误我试图将用户输入转换为数组中的值。

Java翻译错误我试图将用户输入转换为数组中的值。
EN

Stack Overflow用户
提问于 2016-02-20 20:11:35
回答 1查看 90关注 0票数 0

我正在做一个翻译,把英语翻译成一种叫Talhamr的合成语言。要做到这一点,我将用户输入并使其成为数组。然后,我对一个包含英语单词的字库数组进行测试,其中相应的Talhamr单词就在它旁边。如果英语单词有效,我想用相应的Talhamr词代替它。有人能帮我把这个编码吗?贝娄是我到目前为止的密码。

主修班:

代码语言:javascript
复制
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

第二类包含翻译方法:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-02-20 20:43:23

您的问题是使用==比较字符串。因为String是对象,所以检查它们是否是同一个对象,而不是检查它们是否有相同的文本。要正确测试这一点,可以使用string1.equals(string2)

对于您来说,在translateToTalhamr函数中,您应该使用wordBank[j].equals(wordArray[i])而不是wordBank[j] == wordArray[i]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35528428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档