Need help debugging a parallel arrays program.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Need help debugging a parallel arrays program.

  1. #1
    Join Date
    May 2011
    Posts
    3

    Exclamation Need help debugging a parallel arrays program.

    Code:
    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.

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,429

    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 <<<<<<<<<
    Last edited by Norm; May 25th, 2011 at 08:35 PM.
    Norm

  3. #3
    Join Date
    May 2011
    Posts
    3

    Thumbs up Re: Need help debugging a parallel arrays program.

    Thanks for the reply, I am currently going through with some prints checking the the various items in the coding.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2011
    Posts
    3

    Smile Re: Need help debugging a parallel arrays program.

    Thanks for the helps guys, i got the WordAdding up and working, and im currently finishing up the Actual searching of the arraylists.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center