How to loop throug vector
I am trying to develop a simple translation program and need to know how to loop through a vector and check if an input word matches one of the word elements in the vector. I want to output a translation message to screen only if the word is a match (if the word does not match one of the words in my vector I want to output a different message). The code I have created below does work but it outputs a message for every element in the vector. My code is as follows:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Vector1
{
Vector1()
{
Word wd3 = new Word("englishwd3","frenchwd3","meaning3","engexample3","freexample3");
Word wd4 = new Word("englishwd4","frenchwd4","meaning4","engexample4","freexample4");
Word wd5 = new Word("englishwd5","frenchwd5","meaning5","engexample5","freexample5");
Vector<Word> v = new Vector<Word>();
v.add(wd3);
v.add(wd4);
v.add(wd5);
String inputword =JOptionPane.showInputDialog(null, "What word would you like to translate into French?");
for(int i=0; i < v.size();i++)
{
Word wd = v.elementAt(i);
if (inputword.equals(wd.getwdEng()))
{
// create translation message
String message1 = String.format(inputword + " is translated as " + wd.getwdFre() + ". The word means " + wd.getmeaning() +
". An English example: " + wd.getEngEx() + ". A French example: " + wd.getFreEx());
// output translation message
JOptionPane.showMessageDialog( null, message1 );
}
else
{
// create not in dictionary message
String message2 = String.format(inputword + " is not in the dictionary");
// output not in dic message
JOptionPane.showMessageDialog( null, message2 );
}
}
}
public static void main (String[] args)
{
// Start the program running from its constructor
new Vector1();
}
}
Is there any way I can achieve the single output message after looping through the vector?
Thanks.