package Arrays;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Spanish_English_dictonary extends Applet implements ActionListener {
private Button En2Sp, Sp2En, Add;
public boolean clickedEn, clickedSp;
private ArrayList EnDictonary = new ArrayList();
private ArrayList SpDictonary = new ArrayList();
public int PlaceEn, PlaceSp;
TextField EN,SP;
/**
*
**/
private static final long serialVersionUID = 1L;
public void init(){
this.setSize (500,420);
EN = new TextField(25);
add(EN);
EN.addActionListener(this);
SP = new TextField(25);
add(SP);
SP.addActionListener(this);
En2Sp = new Button("English To Spanish:");
add(En2Sp);
En2Sp.addActionListener(this);
Add = new Button("Add the word to the Dictonary:");
add(Add);
Add.addActionListener(this);
Sp2En = new Button("Spanish To English:");
add(Sp2En);
Sp2En.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == En2Sp){
clickedEn = true;
String str = getWord(EN);
PlaceEn = SearchItem(EnDictonary, str);
}
if(e.getSource() == Sp2En){
clickedSp = true;
String str = getWord(SP);
PlaceSp = SpDictonary.indexOf(str);
}
repaint();
}
public void paint(Graphics g){
if(EnDictonary.size() == 0){
EnDictonary.add("House");
EnDictonary.add("Dog");
EnDictonary.add("Hello");
SpDictonary.add("Casa");
SpDictonary.add("Perro");
SpDictonary.add("Hola");
}
displayArrayList(g,EnDictonary, 150);
displayArrayList(g,SpDictonary, 160);
}
public void addWord(){
String e ="",s = "";
e= getWord(EN);
s= getWord(SP);
}
public String getWord(TextField t){
String thisWord=" ";
thisWord = t.getText();
return thisWord;
}
public int SearchItem(ArrayList s, String str){
return s.indexOf(str);
}
public void displayArrayList(Graphics g, ArrayList s, int n){
g.drawString("The words in the dictonary are: " + s, 50, n);
}
}
I am trying to make an applet that uses Parallel arrays to act as a English to Spanish dictionary. I have the above coding which compiles and runs with no errors but i cannot Figure out how to get the translated word to display in its Text-Field. Any and all help is greatly appreciated.
Re: Need help debugging a parallel arrays program.
Try debugging the code by adding println statements to show execution flow.
In the actionPerformed method, chain the if statements by using if and else if AND add a trailing else to show when none of the above if statements were true.
Where in your listener code do you put anything into a text field?
A suggestion for use while testing - preload a text field:
EN = new TextField("Dog", 25); // Pre load for testing <<<<<<<<<
Re: Need help debugging a parallel arrays program.
Parallel arrays are tricky to keep in sync and if you have a large dictionary will be inefficient to search. You would be much better off using a HashMap (or two if you need efficient translation in both directions).
Bookmarks