A better way to do this? (I'm sure there is.)

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();
		
	}
}

For this kind of one-to-one mapping you could have utilized the HashMap class, which allows for key-value mapping. The most straight forward way to implement your one-to-many mapping would be to use use spanish words as a key and a ArrayList with the english meanings as the value.

Thanks. But how do I get random keys? I need to use a random number generator, but I can’t because all of my keys are strings.

Ah ok, I see - overlooked this detail. It’s a bit unfurtunate, that the Map-interface does not provide a get(position) method. I am afraid you have to use a for loop with the entrySet iterator:


Map<String,ArrayList<String>>.Entry entry;
Iterator it= dictionary.entrySet().iterator();
arrayInt = rand.nextInt(dictionary.size());
for(int i=0; i<arrayInt;i++) entry= it.next();

// now entry contains the spanish word as entry.getKey() and the english word list as entry.getValue()
String spanishWord= entry.getKey();
ArrayList<String> englishMeanings= entry.getValue();

There are however myriad other ways to achieve what you want and if you will have a big dictionary, you might be better of using a database.

Why can’t you use String.hashCode()?

[quote=“DecafJava,post:1,topic:31105”]
(List).indexOf((String)) is quite correct, I can certify since I’m using the same feature in my apps. But your random must fit the exact array bounds range, which are [0,arrayList.size() - 1]. So the random must be substracted by one unit. :smiley: