Results 1 to 5 of 5
  1. #1
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default 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:
    Java Code:
    Note: Vector1.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    and the following error when running the program:
    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:
    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();}
    
    }
    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.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

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

  3. #3
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

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

  4. #4
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

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

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    If you are only going from English to French then you could use a Map, with the English word (a String) as the key.

Similar Threads

  1. Need Help Regarding Vectors
    By xHoly in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 07:56 AM
  2. Eigen Vectors
    By koxy in forum Advanced Java
    Replies: 3
    Last Post: 04-11-2010, 10:57 AM
  3. Vectors Problem
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 01-05-2010, 12:16 AM
  4. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •