Results 1 to 5 of 5
Thread: Trouble with Vectors
- 12-07-2010, 05:58 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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:
and the following error when running the program:Java Code:Note: Vector1.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Java 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:
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.Java 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();} }
Thanks.
- 12-07-2010, 06:14 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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);
- 12-08-2010, 11:01 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 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:
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.Java 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(); } }
Thanks.
- 12-08-2010, 11:06 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
Looping with Vectors
Sorry. Code should be:
Java 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(); } }
- 12-08-2010, 11:23 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Need Help Regarding Vectors
By xHoly in forum New To JavaReplies: 6Last Post: 05-23-2010, 06:56 AM -
Eigen Vectors
By koxy in forum Advanced JavaReplies: 3Last Post: 04-11-2010, 09:57 AM -
Vectors Problem
By dashwall in forum New To JavaReplies: 9Last Post: 01-04-2010, 11:16 PM -
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks