-
Trouble with Vectors
Hi. Any help would be appreciated. I am trying to get my head around the use of vectors.
I am trying to create a translation program and as such am trying to use Vectors to store the words (in defferent languages), meanings etc. I have created a Word class and now am trying to store an instance of Word in a vector. I am keeping the code simple for now so that I can get the Vector working before using it in the Tranlator program.
I am getting the following warning when compiling:
Code:
Note: Vector1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
and the following error when running the program:
Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.util.Vector.get(Vector.java:694)
at Vector1.<init>(Vector1.java:11)
at Vector1.main(Vector1.java:15)
The code for my vector program is:
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","frenchexample3","englishexample3");
Vector v = new Vector();
v.add(wd3);
v.get(1);
}
public static void main (String[] args) {
// Start the program running from its constructor
new Vector1();}
}
There is more than likely a simple solution but I need to get an understanding of why this error is occurring and any help would be appreciated.
Thanks.
-
to remove the warning: use Vector<Word> v = new Vector<Word>(); instead of Vector v = new Vector(); (read the generic articles).
to your second problem: the index is zero based (begins at index 0, not 1) ! --> v.get(0);
-
Looping with Vectors
Thanks for the response. That sorted that issue. I have developed the program further but am now having trouple with looping through the Vector to output a particular result. My code is as follows:
Code:
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 " + wdmatch.getwdFre() + ". The word means " + wdmatch.getmeaning() +
". An English example: " + wdmatch.getEngEx() + ". A French example: " + wdmatch.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();
}
}
My intention is to search the vector for a word that matches the inputword. If there is a match, output the translation, meaning and examples. If there is no match, output a message advising so. The problem I have is that the loop is outputting messages for each of the vector entries. What do I need to do so that there is only one output message.
Thanks.
-
Looping with Vectors
Sorry. Code should be:
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();
}
}
-
If you are only going from English to French then you could use a Map, with the English word (a String) as the key.