Some of you may remember me from my thread about the “implements” keyword not working. I finally did get my program to work, but it’s design isn’t so good. I made a vocabulary tester to hone my Spanish skills (I guess it could work with any language) but I think I kind of designed it weird. The way it works is, there are two arrays, one containing Spanish words and the other containing the English equivalent. A random number generator stores an index in a variable arrayInt and then uses that index to pick a random word from the Spanish array. Once something is typed in the text field, the program checks to see if it exists in the English array. If it does, it gets it’s index and compares it with the index of the Spanish word. If they match, it’s counted as correct and the program continues.
The problem is, though, some Spanish words can mean different things. For example, alfrombra can mean carpet or rug. My program can only test for one of those because of the arrays. I know I probably went about this all wrong, but I just see it as an opportunity to improve. ;D Any help is greatly appreciated. Thanks! (Here’s my code. Remember, I probably broke about a million code design rules, but I’m still learning. :))
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener {
Random rand = new Random();
ArrayList<String> dictionary = new ArrayList<String>();
ArrayList<String> englishDict = new ArrayList<String>();
int arrayInt;
JLabel label1 = new JLabel();
JTextField searchBar = new JTextField(25);
public Test() {
super("Tester");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
searchBar.addActionListener(this);
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(label1);
add(searchBar);
// Spanish array
dictionary.add("casa");
// English array
englishDict("house");
arrayInt = rand.nextInt(dictionary.size());
label1.setText(dictionary.get(arrayInt));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (englishDict.indexOf(searchBar.getText()) == (dictionary.indexOf(dictionary.get(arrayInt)))) {
JOptionPane.showMessageDialog(null, "Correct");
searchBar.setText(null);
arrayInt = rand.nextInt(dictionary.size());
label1.setText(dictionary.get(arrayInt));
}
else {
JOptionPane.showMessageDialog(null, "Incorrect, \"" + dictionary.get(arrayInt) + "\" means \"" + englishDict.get(arrayInt) + "\" in English.");
searchBar.setText(null);
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new
com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
}
catch (Exception e) {
}
Test test = new Test();
}
}
