• 0

[JAVA] Passing an Array


Question

I swear, this is the last one for awhile. However, one more bug is stopping me.

So, I'm trying to call this newGame file. In short, the newGame will generate 5 random cards to each of the 4 players. I have one array controlling this, using 3 dimensions to keep track of each card. (User, Card Number, Suite Number) I know, probably not the easiest way to do this, but it's how I'm attempting.

Anyways, all I'm trying to do is to pass the grab the return array, which for whatever reason, I cannot seem to get.

Play.java

public void newGameMethod() //starts game
	{
		newGame start = new newGame();
		cards = start.deal(); //works when this line is commented out... 

		System.out.println("test");

	}

When I comment out "cards = start.deal();", the println works.

newGame.java

import java.util.Random;

public class newGame
{
	public static int[][][] deal()
	{
		Random generator = new Random();

		int[][][] cards = new int[4][5][4]; //Person, Card, Suit

		<!--SNIP-->

		return cards;

	}
}

What am I doing wrong where I can't grab the array from newGame.

Thanks for the help. I know, this one is just me missing something so simple, I know it.

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Read a bit about OOP (object oriented programming), that's not how you should go about building a card game at all. I didn't really try to figure out why your array passing isn't working but if you do it properly (in OOP) then you won't need to worry about it

Link to comment
Share on other sites

  • 0

In Play.java it looks like you haven't declared the variable "cards", you've only assigned it a value. You've got to declare the variable as well. I could be wrong about this of course, but I don't know what the rest of Play.java looks like so I can't be sure. Anyhow, assuming you haven't already declared the variable "cards" as type int[][][],... try this:

replace

		cards = start.deal(); //works when this line is commented out...

with this:

	int[][][] cards = start.deal();

.

By the way, Rudy is right. Instead of representing cards as a 3-dimensional array, you should probably define a Card class with three [int] instance variables. Then you don't have to deal with mucky arrays. :)

Hope that helps! If this doesn't solve the problem, it'd help if you could post the entire contents of Play.java as well as letting us know the specific error message you encounter when trying to run this code.

dt

Link to comment
Share on other sites

  • 0

I forgot to include the initializing the variable line, but I do have it, just farther up, outside of that method.

I'll look into the OOP tomorrow, too late to learn new stuff tonight. But, I still would like to know why I can't seem to get this to work.

Link to comment
Share on other sites

  • 0

Shouldn't you call the 'deal method' the static way?

public void newGameMethod() //starts game
	{
		//newGame start = new newGame();
		cards = newGame.deal(); //works when this line is commented out... 

		System.out.println("test");

	}

There could be something else you are doing wrong as well, but that might be a logical error.Doing the above can sort out the static problem I guess.

Edit- or you could just delete the static keyword of 'deal' method. :)

Edited by Aquarian
Link to comment
Share on other sites

  • 0

If the print isn't working at all that implies it's not being executed. Do you have any try/catch blocks with a catch that does NOT print the exception?

Link to comment
Share on other sites

  • 0

dt- No errors at all. Just it's not being executed. It compiles and runs, but the test println doesn't happen. But no error messages, anywhere. That's what is making this hard.

Aquarian- Just tried both ways you mentioned, no luck.

JamesCherrill- I'm not the most familiar with try/catch, but the only one I have is for the background image to load:

public Play()
	{
		playArea = new Frame("Euchre");
		try
		{
			backgroundImg = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("background.gif"), "background.gif"));
		}
		catch (Exception e) {  }
	}

Link to comment
Share on other sites

  • 0

catch (Exception e) { }

means: if there is a problem just ignore it and don't tell me anything.

catch (Exception e) { e.printStackTrace(); }

means: if there is a problem tell me exactly what it was and where it happened.

Suggest you try the second version!

Link to comment
Share on other sites

  • 0

Alright.

But don't change the code that I posted there, that is the correct syntax.The reason you didn't already get a compile error is most likely because the newGameMethod() method isn't being called whatsoever.

You also said println("test") doesn't execute, right? That pretty much confirms the above assumption.

So in order to help us -and yourself- understand what's going wrong, I think you need to tell us where you call newGameMethod() .Please post the related code block and any other code that affects that block.

Link to comment
Share on other sites

  • 0

Do you have the newGame class within the same file as the newGameMethod function? I seemed to be able to make it work by making a couple of things static (and vice-versa):

import java.util.Random;

public class Test {

	public static void main(String[] args) {

		// TODO, add your application code
		System.out.println("Hello World!");
		newGameMethod();
	}

	public static void newGameMethod() //starts game
	{
		newGame start = new newGame();
		int[][][] cards = start.deal(); //works when this line is commented out...

		System.out.println("test");

	}


	public static class newGame
	{
		public int[][][] deal()
		{
			Random generator = new Random();

			int[][][] cards = new int[4][5][4]; //Person, Card, Suit


			return cards;

		}
	}
}

The above code works for me.

Link to comment
Share on other sites

  • 0

JamesCherrill- The try/catch is only around the image loading file. So, wouldn't it only prevent errors from the failure to load the image? Either way, still doesn't work.

Aquarian- The method is being called. When I take out "cards = start.deal();", the test prints out. However, when I add "cards = start.deal();", the test does not appear. So, "cards = start.deal();" is breaking the script.

dlegend- It's not in the same file, but I wouldn't have a problem moving it if it works that way. I'll let you know.

Edit: It still doesn't seem to work. Same thing as before, compiling, test isn't printing, unless I comment out those lines.

Edited by Hendrick
Link to comment
Share on other sites

  • 0

This is getting kinda weird.

I just tested the modified code of dlegend and it worked all the way, it also ran correctly even when it was broken into three classes.The "test" string prints out anyhow.

Also, I was being wrong about he static thing.Actually, I tested your original code in C# and it raised an error during compile-time.

In Java though, it's different if i'm not mistaken.you can call a static method with an object of its class, yet you can't call a non-static method the static way i.e by using the class name.That means your code was correct in that regard.

Anyways I don't really have further thought on this at the moment.

The only thing that I can think of right now and it may sound absurd, is to delete all the produced class files and compile anew.Sometimes, java compiler caches the class files and doesn't overwrite them after a re-compile and this gives rise to some rather annoying errors.

Edited by Aquarian
Link to comment
Share on other sites

  • 0

That didn't work either, but let me copy what I currently have, just incase I forgot something:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.Random;

public class Play extends JPanel implements ActionListener
{
	Frame playArea;								//frame variable
	public Image backgroundImg;					//background image
	//int[][][] cards = new int[4][5][4];	//cardlist

	public Play()
	{
		playArea = new Frame("Euchre"); //Creates Frame with Name
		try
		{
			backgroundImg = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("background.gif"), "background.gif"));
		}
		catch (Exception e) { e.printStackTrace(); }
	}

	public void launch()
	{
		playArea.add(this);
		playArea.setSize(900,600); //sets size WxH
		playArea.setVisible(true); //lets see it
		playArea.requestFocus();   //Takes Focus
		newGameMethod();
	}

	protected void paintComponent(Graphics g)   //Background Image, whole section
	{
		super.paintComponent(g);
		if (g != null)
			g.drawImage(backgroundImg, 0,0,this.getWidth(),this.getHeight(),this);
	}

	public void actionPerformed(ActionEvent event) //Needed for Action Listener
	{
		//do nothing
	}


	public static void newGameMethod()
	{

		newGame start = new newGame();  //line in question
		int[][][] cards = start.deal();   //line in question

		System.out.println("test");

	}

	public static class newGame
	{
		public int[][][] deal()
		{
			Random generator = new Random();

			int[][][] cards = new int[4][5][4]; //Person, Card, Suit
			cards[0][0][0] = 0;

			<!--Useless Code Snip-->

			return cards;

		}
	}

	public static void main (String[] args)  //displays
	{
		Play go = new Play();
		go.launch();

	}
}

Thats pretty much the whole code if you want to pick it over any more.

Link to comment
Share on other sites

  • 0

Your current code prints "test" and displays a non-responsive form, which I had to kill with the task manager. I've no experience with Java GUIs but I suppose you're missing some code to make the form actually respond to user interaction. Also, your design is questionable: having a method called newGameMethod() and a static class called newGame? Why are they static? Why is your game logic in the same class as GUI elements?

Otherwise I don't see any major problems with the code, did you try stepping it with the debugger to see exactly what's going on?

Link to comment
Share on other sites

  • 0

That code, exactly, prints out a "test" message for you? Very interesting. That is not the case here.

They are static because somewhere above I was told to try that, to see if it would work. It didn't.

Link to comment
Share on other sites

  • 0
That code, exactly, prints out a "test" message for you? Very interesting. That is not the case here.

They are static because somewhere above I was told to try that, to see if it would work. It didn't.

Yeah I created a new Java Project in eclipse and copy-pasted your code. I commented out the line "<!--Useless Code Snip-->" since it's not java code and ran it. In the console output it showed "test".

Did you try stepping your code through the debugger?

Link to comment
Share on other sites

  • 0

I talked to my former Java teacher about this and after looking over your code in the original post, he was pretty sure that something is preventing the normal flow of program in the snipped code, in the 'deal' method.

That's why when you comment out start.deal() the program functions as it should and prints out the "test" string.We didn't have that sipped part and thus, there was no problem with the program in our computers.

Why don't you just post that snipped block? It's not like it's a porno code or something lol!

Link to comment
Share on other sites

  • 0

I think I may of finally figured it out. After Dr_Asik reported it worked for him, I decided the issue was either with the compiler (unlikely), or, like your Java teacher stated Aquarian, an issue with the snipped out code. Turns out the loop was running one too many times, but the error, for whatever reason, wasn't displaying anywhere, something I've never experienced when I ran a loop one too many times.

Thanks for all the help everyone. I couldn't figure it out why it wasn't working. I feel horrible though since it was an issue I wasn't posting for everyone to see.

Link to comment
Share on other sites

This topic is now closed to further replies.