CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: arrays

  1. #1
    Guest

    arrays

    why does this sample applet not work? anything wrong with the array declaration??

    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;

    public class dictionary extends Applet
    {
    TextField s;
    int row=4;
    int col=4;
    //private String[][] dict = new String;
    String[][] dict=new String[row][col];
    dict[0][0]="man";
    dict[0][1]="book";
    dict[0][2]="girl";
    dict[0][3]="pen";
    dict[1][0]="homme";
    dict[1][1]="livre";
    dict[1][2]="fille";
    dict[1][3]="stylo";

    s=new TextField(8);
    add(s);
    s.addActionListener(this);

    public void actionPerformed (ActionEvent e) {
    // event handling for event comes from the textfield
    if (e.getSource() == s) {
    Graphics g = getGraphics(); // u
    }

    for (int s=0;(!dict[s].column.equals(s))
    &&
    (!dict[s].column.equals("END"));
    s++)
    {}

    if (dict(s).column.equals("END"))
    {
    g.drawString("Word not found",10,10);
    else
    g.drawString("The French word for"+s+"is"+dict[s][(row+1)],10,10);
    }
    }
    }
    }







  2. #2
    Join Date
    Mar 2000
    Location
    Dublin, Ireland
    Posts
    124

    Re: arrays

    WhoeverYouAre,

    No offence but the code you've written is a confusing mess...


    So I've rewritten it as best I could...
    import java.awt.*;
    import java.applet.*;

    public class MyApplet extends Applet
    {
    // Place global class members here:
    String[][] dictionary = new String[4][4];
    java.awt.TextField wordTF = new java.awt.TextField();

    public void init()
    {
    // initialize your String array:
    dictionary[0][0]="man";
    dictionary[0][1]="book";
    dictionary[0][2]="girl";
    dictionary[0][3]="pen";
    dictionary[1][0]="homme";
    dictionary[1][1]="livre";
    dictionary[1][2]="fille";
    dictionary[1][3]="stylo";
    // initialize and add your TextField 'wordTF' :
    add(wordTF);
    }

    public void actionPerformed(ActionEvent e)
    {
    // there was an event in the 'wordTF' TextField
    if ( e.getSource() == wordTF )
    {
    String frenchWord = ""; // declare a String to hold the french word
    String englishWord = wordTF.getText(); // declare a String to hold the english word

    // now loop through your dictionary...
    // if there is a match, assign 'frenchWord' with the proper index
    for ( int column = 0 ; column < 4 ; column++ )
    if ( dictionary[0][column].equals(englishWord) )
    frenchWord = dictionary[1][column];

    // check if 'frenchWord' has anything in it
    // if not, then the word was not found...
    if ( frenchWord.length() == 0 )
    frenchWord = englishWord + " not found";

    // call your Graphics and drawString methods here...
    }

    }
    }



    Keep an eye on where you instanciate and initiailize your variables.
    Look closely at how arrays are indexed.

    Hope this helps,

    Regards,
    dogBear





  3. #3
    Guest

    Re: arrays

    hi dogbear.

    that corrected my dictionary applet to some extent..
    but it still shows errors in lines 27, 28 and 30...


    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;

    public class dictionary extends Applet implements ActionListener{//placing global class members here
    String[][] dict=new String[4][4];

    TextField s=new TextField();

    public void init() {//Initialising the string array

    dict[0][0]="man";
    dict[0][1]="book";
    dict[0][2]="girl";
    dict[0][3]="pen";
    dict[1][0]="homme";
    dict[1][1]="livre";
    dict[1][2]="fille";
    dict[1][3]="stylo";
    add(s);//add the TextField here
    }

    public void actionPerformed(ActionEvent e) { ///this refers to an event in the 's' TextField
    if (e.getSource()==s) {
    String frenchWord="";//declare a string to hold the french word
    String englishWord=s.getText();//declare a string to hold the English word from the textfield
    frenchWord=dict[1][column];//If not, the word is not found
    public void paint(Graphics g) {
    g.drawString("French word:"+frenchWord,50,50);
    }//ends graphics
    } //ends condition 'if "s"'
    if(frenchWord.length()==0){
    frenchWord=englishWord + "is not found in our dictionary";
    }
    }//end of action event e
    }//end of applet






  4. #4
    Join Date
    Mar 2000
    Location
    Dublin, Ireland
    Posts
    124

    Re: arrays

    Anonymous,

    What are the errors reported?

    dogBear


  5. #5
    Guest

    Re: arrays

    errors are pointed as follows..


    frenchWord=dict[1][column];/*It shows and error here.. I didnt understand the
    message. But I suspect It doesnt recognise"column"..*/

    Public void paint(Graphics g) { //>here it says statement expeted

    g.drawString("French word:"+frenchWord,50,50);//>here it says "type expeceted"








  6. #6
    Join Date
    Mar 2000
    Location
    Dublin, Ireland
    Posts
    124

    Re: arrays

    Anonymous,

    Looks as if I'll have to do it all for you...

    First of all, you are calling the Graphics.drawString(...) method from your ActionPerformed() method.
    There's no Graphics object there!!!
    You must call paint() if you want the result drawn and you must have a global String variable in your applet so that when paint() is called, the public void paint(Graphics g) that you've overridden can have a copy of the result in it!

    So, after all that, here's an Applet that will do what you want...
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class MyApplet extends Applet implements ActionListener {
    // Place global class members here:
    String[][] dictionary = new String[4][4]; //the dictionary
    java.awt.TextField wordTF = new java.awt.TextField(); //the input field
    String searchResult = ""; //where you put the search results

    public void init()
    {
    // initialize your String array:
    dictionary[0][0]="man";
    dictionary[0][1]="book";
    dictionary[0][2]="girl";
    dictionary[0][3]="pen";
    dictionary[1][0]="homme";
    dictionary[1][1]="livre";
    dictionary[1][2]="fille";
    dictionary[1][3]="stylo";
    // initialize the Applet itself
    setLayout(null);
    setBounds(0,0,300,200);
    // initialize and add your TextField 'wordTF' :
    add(wordTF);
    wordTF.setBounds(10,10,60,18);
    wordTF.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
    // there was an event in the 'wordTF' TextField
    if ( e.getSource() == wordTF )
    {
    String frenchWord = ""; // declare a String to hold the french word
    String englishWord = wordTF.getText(); // declare a String to hold the english word
    // now loop through your dictionary...
    // if there is a match, assign 'frenchWord' with the proper index
    for ( int column = 0 ; column < 4 ; column++ )
    if ( dictionary[0][column].equals(englishWord) )
    frenchWord = dictionary[1][column];
    // check if 'frenchWord' has anything in it
    // if not, then the word was not found...
    if ( frenchWord.length() == 0 )
    frenchWord = englishWord + " not found";

    // store the result into the GLOBAL String object 'searchResult'
    searchResult = frenchWord;
    // call repaint()... which, in turn, will call your paint() method
    repaint();
    }
    }

    public void paint(Graphics g)
    {
    // have the applet draw the other things you don't care about...
    super.paint(g);
    // now draw your search results(which is stored in 'searchResult')
    g.drawString(searchResult,10,40);
    }
    }



    The Applet will look like it's been designed by my dead cat, but you can fix that easily.

    Regards,
    [b]dogBear[b]



Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured