-1

I'm trying to make a program that will translate English words to Japanese and vice versa that are listed in the given arrays. It will also display the image for the given word. For my actionPerformed method, I coded it to be when the "English to Japanese" button is pressed the for-loop will run until the input from the JTextField is the same as the words listed in the englishWords array. But I'm not sure if that's right. Is it my for-loop that is incorrect or am I approaching the code wrong?

P.S. ignore the non-completed image code, I'm just trying to figure out how to compare the words together

public class Translator extends JApplet implements ActionListener
{

//English array
String[] englishWords = { "Fish", "Lettuce", "Bike", "Chef", "Teacher" };    

//Japanese array
String[] japaneseWords = { "Sakana", "Retasu", "Jitensha", "Shefu", "Sensei" };

//Image array
Image[] wordImages;
Image wrong;

//Java util
JButton english, japanese;
ImageIcon icon;
JLabel label;
JTextField words;

public void init()
{

    //Images
    wordImages = new Image[5];
    wordImages[0] = getImage(getCodeBase(), "Fish.jpg");
    wordImages[1] = getImage(getCodeBase(), "Lettuce.jpg");
    wordImages[2] = getImage(getCodeBase(), "Bike.jpg");
    wordImages[3] = getImage(getCodeBase(), "Chef.jpg");
    wordImages[4] = getImage(getCodeBase(), "Sensei.jpg");
    wrong = getImage(getCodeBase(), "Wrong.jpg");

    //Layout
    setLayout(new FlowLayout());
    words = new JTextField(null, 10);
    english = new JButton("English to Japanese");
    japanese = new JButton("Japanese to English");
    icon = new ImageIcon(wordImages[0]);
    label = new JLabel( icon, JLabel.CENTER);
    icon.setImage(wordImages[0]);
    label.setIcon(icon);

    //ActionListener
    english.addActionListener(this);
    japanese.addActionListener(this);

    //Install
    add(words);
    add(english);
    add(japanese);
    add(label);
}
public void actionPerformed( ActionEvent ae )
{
    Object src = ae.getSource();
    if(src == english)
    {
        for(int x = 0; x < englishWords.length; ++x)
            if(englishWords[x] == words.getText())
            icon.setImage(wordImages[x]);
    }
    else if(src == japanese)
    {
        for(int x = 0; x < japaneseWords.length; ++x)
            if(japaneseWords[x] == words.getText())
                icon.setImage(wordImages[x]);

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Its `if(englishWords[x].equals(words.getText()))` – SatyaTNV Mar 24 '16 at 06:43
  • 2
    *"ignore the non-completed image code"* Rather than telling us what to ignore, don't include it. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 24 '16 at 06:53

2 Answers2

-1

Use .equals instead of == because you are creating String from text filed "dynamically" and hence it won't be allocated the same memory as it gets allocated when you do:

String s1 = "Fish";
String s2="Fish"l
Optional
  • 4,387
  • 4
  • 27
  • 45
-1

== compare object not string literally. You must use .equals

Refer this

Java, how to compare Strings with String Arrays

Community
  • 1
  • 1
Vrill7
  • 46
  • 4