Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

question on multidimensional array

Thread view: 
Huub - 30 May 2004 08:42 GMT
Hi,

I'm trying to pass an element of a multidimensional array to a method in
a different class. However, I get these errors:

> Spel.java [82:1] illegal start of expression
>                     speelveld.plaatsPion([0][0]);
>                                          ^
> Spel.java [82:1] ')' expected
>                     speelveld.plaatsPion([0][0]);
>                                                 ^
The method in the other class is this:
> private int vak[][] = new int[3][3];
>
> public int plaatsPion(int vak[][])
> {
> }

Can somebody please explain what's wrong?

Thanks

Huub
Andrew Thompson - 30 May 2004 08:38 GMT
> I'm trying to pass an element of a multidimensional array to a method in
> a different class. However, I get these errors:
<snip snippets>

Did you ever get the time to read the
article to which I linked here?
<http://google.com/groups?th=46bbdccdb19df268#link2>

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Huub - 30 May 2004 09:18 GMT
>>I'm trying to pass an element of a multidimensional array to a method in
>>a different class. However, I get these errors:
[quoted text clipped - 4 lines]
> article to which I linked here?
> <http://google.com/groups?th=46bbdccdb19df268#link2>

Yes, and I thought I complied to that by giving the codes and
error-messages. I assume I should provide more code?
Andrew Thompson - 30 May 2004 13:34 GMT
...
>>>I'm trying to pass an element of a multidimensional array to a method in
>>>a different class. However, I get these errors:
...
>> <http://google.com/groups?th=46bbdccdb19df268#link2>
>>
> Yes, and I thought I complied to that by giving the codes and
> error-messages. I assume I should provide more code?

SSC'C'E  - Compileable.. which comes from being
S'SC'CE  - Self Contained..

The code you posted was not
copy/paste/compile..

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Tony Morris - 30 May 2004 09:47 GMT
> Can somebody please explain what's wrong?

There is a common misconception among Java developers that they are using a
multi-dimensional array.
Be it known that Java does not have the ability for multi-dimensional
arrays.
It does, however, allow arrays of arbitrary type, which includes arrays of
arrays and arrays of arrays of arrays and so on.
This distinction is important, and training yourself to understand this fact
will certanily assist you in solving your problem. In your case, you have an
array, and that array holds arrays (say it a few times to yourself).

A language such as COBOL supports true multi-dimensional arrays (known as
"tables" in that context). A full explanation is pending on my Java FAQ web
page "when I get around to it".
http://www.xdweb.-net/~dibblego/java/faq

Hope this helps.

Signature

Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)

Ryan Stewart - 30 May 2004 13:22 GMT
> Hi,
>
[quoted text clipped - 20 lines]
>
> Huub

Andrew was trying to get you to post compileable code that demonstrates your
problem. From what you have here I get the vague idea that you should be
doing this:
speelveld.plaatsPion(vak[0][0]);

at line 82.
Huub - 30 May 2004 16:16 GMT
>>Hi,
>>
[quoted text clipped - 28 lines]
>
> at line 82.

That's what solved that error, only to give another one back:
> Spel.java [82:1] plaatsPion(int[][]) in Speelveld cannot be applied to (int)
>                     speelveld.plaatsPion(vak[0][0]);
>                              ^
> 1 error
> Errors compiling Project BKE.

Want me to post the entire code (I don't think I can snip out much
without making it incompilable)?
Joachim - 30 May 2004 21:51 GMT
> > Andrew was trying to get you to post compileable code that demonstrates your
> > problem. From what you have here I get the vague idea that you should be
[quoted text clipped - 9 lines]
> > 1 error
> > Errors compiling Project BKE.

What about this:
speelveld.plaatsPion(vak);

Signature

Joachim

Oscar kind - 30 May 2004 22:28 GMT
[...]
>>>>public int plaatsPion(int vak[][])
[...]
>> Andrew was trying to get you to post compileable code that demonstrates your
>> problem. From what you have here I get the vague idea that you should be
[quoted text clipped - 7 lines]
>>                     speelveld.plaatsPion(vak[0][0]);
>>                              ^

You may want to go back to the design phase: the method plaatsPoin only
gets the board, but not the coordinates... Only giving it a board (vak)
or the current status of a coordinate (vak[0][0]) will not help.

An option might be:
    public void plaatsPion(int vak[][], int rij, int kolom);

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

Ryan Stewart - 30 May 2004 23:21 GMT
> Want me to post the entire code (I don't think I can snip out much
> without making it incompilable)?

No, that's the point. Ask yourself, "What is my problem?" Apparently your
answer is that you aren't sure of the syntax for passing multidimensional
arrays to a method. So you could write a simple class with a method into
which you attempt to pass a multidimensional array. Then you could post that
code and the error it gives you and say, "I want to pass this array to this
method, but it gives me this error. Why?" Then we could give you immediate
help. More generally: 1) Either cut out the code that isn't problematic or
write a small program that demostrates your problem. 2) Post the code,
expected results, and actual results or error. That's the gist of Andrew's
SSCCE (if I got the acronym right). Doing this typically allows us to look
at your code and know what's wrong in about ten seconds.
Andrew Thompson - 31 May 2004 05:34 GMT
> That's the gist of Andrew's
> SSCCE (if I got the acronym right).

You did.  

Short, Self contained, Complileable, Example.

To Huub.  My Newsreader lists posts
by how many lines they are.

I just noticed, skimming the group that someone
posted a 173 line question, and one of the more
experienced members of the group posted a single
250 line reply.

Neither of these posts is extraordinary.

[ But the shorter the better.. ]

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Huub - 31 May 2004 08:10 GMT
>>That's the gist of Andrew's
>>SSCCE (if I got the acronym right).
[quoted text clipped - 14 lines]
>
> [ But the shorter the better.. ]

OK, here's the code (4 classes, error is with the 1st and 2nd). I hope
I'm not trying to do something impossible.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.awt.Color.*;

public class Spel extends JFrame implements ActionListener
{
    boolean gestart;
    boolean beeindigd;
    int kleur;
    private JMenuItem jmiExit, jmiNew, jmiSettingsHelpSubMenu,
jmiColourHelpSubMenu, jmiColour, jmiColourForeground, jmiBlue;
    private JRadioButtonMenuItem jrbmiBlue, jrbmiRed, jrbmiGreen;
    char speler;
    private char beurt = 'X';
    private JButton[] knoppen = new JButton[10];
    private JPanel paneel = new JPanel(); // maak paneel om het menu en
de knoppen in te plaatsen
    private JPanel paneel2 = new JPanel();
    Container container = getContentPane(); // get het contentpane van
het frame
    JMenuBar jmb = new JMenuBar(); // maak een menu aan
    Speelveld speelveld = new Speelveld();
    int vak[][] = new int[3][3];

    /** Creates a new instance of Spel */
    public Spel()
    {
        container.setLayout(new BorderLayout()); // set de borderlayout
voor het frame
        paneel.setLayout(new GridLayout(3,3)); // definieer een
GridLayout van 3 bij 3
        for (int i = 1; i <= 9; i++) // voeg de vakjes toe aan paneel
                knoppen[i] = new JButton("");
        for (int i = 1; i <= 9; i++) // voeg de vakjes toe aan paneel
                paneel.add(knoppen[i]);
        paneel2.setLayout(new BorderLayout()); // hierin  komen knoppen
en het menu
        JMenu fileMenu = new JMenu("File"); // maak menu-item File aan
        JMenu settingsMenu = new JMenu("Settings"); // maak menu-item
Settings aam
        jmb.add(fileMenu); // voeg File em Settings toe aan het Menu
        jmb.add(settingsMenu);
        fileMenu.add(jmiNew = new JMenuItem("New")); // voeg Filemenu
items toe
        fileMenu.addSeparator();
        fileMenu.add(jmiExit = new JMenuItem("Exit"));
        fileMenu.setMnemonic('F'); // voeg Filemenu mnemonics toe
        jmiNew.setMnemonic('N');
        jmiExit.setMnemonic('x');
        JMenu colourHelpSubMenu = new JMenu("Colour"); // voeg
Settingsmenu items toe
        settingsMenu.add(jmiColour = colourHelpSubMenu);
        colourHelpSubMenu.add(jrbmiBlue = new
JRadioButtonMenuItem("Blue")); // voeg kleur radiobuttons toe
        colourHelpSubMenu.add(jrbmiRed = new JRadioButtonMenuItem("Red"));
        colourHelpSubMenu.add(jrbmiGreen = new
JRadioButtonMenuItem("Green"));
        settingsMenu.setMnemonic('S'); // voeg Settingsmenu mnemonics toe
        jmiColour.setMnemonic('C');
        jrbmiBlue.setMnemonic('B');
        jrbmiRed.setMnemonic('R');
        jrbmiGreen.setMnemonic('G');
        paneel2.add(jmb, BorderLayout.NORTH); // voeg het Menu toe aan
het borderlayout
        paneel2.add(paneel, BorderLayout.CENTER);
        container.add(paneel2, BorderLayout.CENTER); // voeg paneel toe
aan het frame
        jmiExit.addActionListener(this); // registreer listeners
        jrbmiBlue.addActionListener(this);
        jrbmiRed.addActionListener(this);
        jrbmiGreen.addActionListener(this);
        ButtonGroup btg = new ButtonGroup(); // voeg buttongroup toe
        btg.add(jrbmiBlue);
        btg.add(jrbmiRed);
        btg.add(jrbmiGreen);
        knoppen[1].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[1] = new JButton("");
                    System.out.println("1");
                    // zet vak[0][0] op 'gevuld' met beurt;
                    speelveld.plaatsPion(vak[0][0]);
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[2].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[2] = new JButton("x");
                    System.out.println("2");
                    // zet vak[1][0] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[3].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[3] = new JButton("3");
                    System.out.println("3");
                    // zet vak[2[[0] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[4].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[4] = new JButton("4");
                    System.out.println("4");
                    // zet vak[0][1] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[5].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[5] = new JButton("x");
                    System.out.println("5");
                    // zet vak[1][1] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[6].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[6] = new JButton("x");
                    System.out.println("6");
                    // zet vak[2][1] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[7].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[7] = new JButton("x");
                    System.out.println("7");
                    // zet vak[0][2] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[8].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[8] = new JButton("x");
                    System.out.println("8");
                    // zet vak[1][2] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
        knoppen[9].addActionListener(
            new ButtonListener() {
                public void actionPerformed(ActionEvent e)
                {
                    knoppen[9] = new JButton("x");
                    System.out.println("9");
                    // zet vak[2][2] op 'gevuld' met beurt;
                    wisselSpeler();
                    System.out.println(beurt + " heeft de beurt");
                }
            }
        );
    }

    public void startSpel()
    {
    }

    public void eindeSpel()
    {
    }

    public void eindeProgramma()
    {
        System.exit(0);
    }

    public char wisselSpeler()
    {
        if (beurt == 'X')
        {
            beurt = 'O';
            System.out.println("De beurt is gegaan naar " + beurt);
        }
        else if (beurt == 'O')
        {
            beurt = 'X';
            System.out.println("De beurt is gegaan naar " + beurt);
        }
        System.out.println("Beurt = " + beurt);
        return beurt;
    }

    public static void main(String[] args)
    {
        Spel spel = new Spel();
        spel.setTitle("Boter, Kaas en Eieren");
        spel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        spel.setSize(200, 200);
        spel.setVisible(true);
    }

    class Vakjes extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            if (speler == 'x')
                g.drawOval(10, 10, getSize().width-20,
getSize().height-20);
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();

        if (e.getSource() instanceof JMenuItem)
            if ("Exit".equals(actionCommand))
            {
                eindeProgramma();
            }
        if (e.getSource() instanceof JRadioButtonMenuItem)
            if ("Blue".equals(actionCommand))
            {
                paneel.setForeground(Color.blue);
                System.out.println("Blue");
            }
            if ("Red".equals(actionCommand))
            {
                paneel.setForeground(Color.red);
                System.out.println("Red");
            }
            if ("Green".equals(actionCommand))
            {
                paneel.setForeground(Color.green);
                System.out.println("Green");
            }
        if (e.getSource() instanceof JButton)
            System.out.println("Button clicked");
    }

    class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        }
    }
}

public class Speelveld
{
    int vakafmeting;
    int getal;
    String vakBezetDoor;
    boolean speelveldVol;
    boolean speelveldLeeg;
    final static int max_aantal_horizontaal = 3;
    final static int max_aantal_vertikaal = 3;
    int aantalPionnenOpSpeelveld;
    private int vak[][] = new int[3][3];

    /** Creates a new instance of Speelveld */
    public Speelveld()
    {
    }

    public int controleerBezetting(int vak[][])
    {
        System.out.println(vak);
        return getal;
    }

    public int plaatsPion(int vak[][])
    {
        System.out.println("vak["+"]["+"]");
        return getal;
    }

    public void verwijderPion()
    {
    }
}

public class Speler
{
    String naam;
    String symbool;

    /** Creates a new instance of Speler */
    public Speler()
    {
    }

}

public class Pion
{
    int vorm;

    /** Creates a new instance of Pion */
    public Pion()
    {
    }

}
Andrew Thompson - 31 May 2004 09:24 GMT
>> Short, Self contained, Complileable, Example.
>>
[quoted text clipped - 9 lines]
>>
>> [ But the shorter the better.. ]

( hinting at shorter than 173 lines.. )

> OK, here's the code (4 classes,

The example only requires two.  If you can
pass a 2D arary between one class and another,
you can do it between 6, 10 or 50..

>..error is with the 1st and 2nd). I hope
> I'm not trying to do something impossible.

You are attempting to do something fairly
easy (from your description) but the
'penny has not dropped' yet.  You will
kick yourself when it finally makes
sense, you will _then_ wonder how you
ever misunderstood it.   ;-)

> import javax.swing.*;
...
>          knoppen[9].addActionListener(

OK.  I am gonna break in here.  

Somebody else may wade through your code,
but I do not have the time to wade through
your 4 classes, spread over a 357 line post,
especially when the source needs to be broken
into separate files to compile, and includes
other compile errors in addition to that..

C:\Spel.java:19: ';' expected.
de knoppen in te plaatsen
         ^
C:\Spel.java:19: Duplicate variable declaration: de knoppen was JButton
knoppen[]
de knoppen in te plaatsen
          ^
C:\Spel.java:22: ';' expected.
het frame
        ^
C:\Spel.java:31: ';' expected.
voor het frame
       ^
C:\Spel.java:39: ';' expected.
en het menu
     ^
C:\Spel.java:274: Public class Speelveld must be defined in a file called
"Speelveld.java".
public class Speelveld
            ^
C:\Spel.java:308: Public class Speler must be defined in a file called
"Speler.java".
public class Speler
            ^
C:\Spel.java:320: Public class Pion must be defined in a file called
"Pion.java".
public class Pion
            ^
8 errors

....
Your SSCCE should be trimmed to under 100 lines.

You can make the 'public' classes 'default' so
that they compile in a single file.

Please have a _careful_ read of the SSCCE document
and you will note that you do not need a UI for this..
Especially not an UI with 9(!) buttons.

As I recall, I provided a self contained
example earlier..  Yeah.. there it is..
<http://groups.google.com/groups?th=7995d61159b0265c#link2>
less than 40 lines of code that can be copy/pasted/compiled/run
that demonstrate passing a 2D array between classes.

Also check Woebegone's even shorter example,
immediately below that post.

It is important to understand how you can
trim code down to the *simplest* code that
demonstrates a problem or solution.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Huub - 31 May 2004 09:59 GMT
>>>Short, Self contained, Complileable, Example.
>>>
[quoted text clipped - 95 lines]
> trim code down to the *simplest* code that
> demonstrates a problem or solution.

Alright. I'll have a thorough look at it once again. Then see if I
should make another post or reply. 8 Errors....I really got only 1. But
maybe this 1 holds back the others as yet.
Thanks so far.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.