10 Replies - 1860 Views - Last Post: 06 September 2011 - 08:14 AM Rate Topic: -----

#1 javojava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-August 11

getting 'void' type not allowed here error

Posted 05 September 2011 - 03:55 PM

Hi guys I am getting the following error:

C:\Program Files\Java\jdk1.6.0_24\bin>javac Question4Simulation.java
.\World.java:134: 'void' type not allowed here
if (grid[i][j] != null && grid[i][j].move()) {
^
1 error

Line 134...that part of the code was provided by the instructor, and I am lost here. Any help would be greatly appreciated.

/** World.java
 * The World class stores data about the world by creating a
 * WORLDSIZE by WORLDSIZE array of type Organism.
 * Null indicates an empty spot, otherwise a valid object
 * indicates an ant or doodlebug.  To determine which,
 * invoke the virtual function getType of Organism that should return
 * ANT if the class is of type ant, and DOODLEBUG otherwise.
 */
public class World {
	public static final int WORLDSIZE = 20;
	
	/** 2-D Array of Organisms that make up a WORLDSIZE x WORLDSIZE
	 *  grid.  Some x,y positions may be null, some may contain Organisms
	 *  of Ant or Doodlebug types.
	 */ 
	private Organism[][] grid;

	/** Empty constructor.  
	 *  Initialize the grid to contain no organisms
	 */
	public World()	{
	   grid = new Organism[WORLDSIZE][WORLDSIZE];
	}

	/** Returns the Organism stored in the grid array at x,y.
	 * @param x - X grid coordinate 
	 * @param y - Y grid coordinate 
	 * @return Organism - organism occupying this x,y coordinate of the
	 *  grid, or NULL if no organism is at that location.
	 */
	public Organism getAt(int x, int y)	{
		if ((x >= 0) && (x < World.WORLDSIZE) && 
		    (y >= 0) && (y < World.WORLDSIZE)) {
			return grid[x][y];
		}
		
		// return null for out of bounds checks
		return null;
	}

	/** Puts the Organism on the grid at x,y.
	 * @param x - X grid coordinate 
	 * @param y - Y grid coordinate 
	 * @param org (Organism) - to place on the grid
	 */
	public void setAt(int x, int y, Organism org) {
	   if ((x >= 0) && (x < World.WORLDSIZE) && 
         (y >= 0) && (y < World.WORLDSIZE))	{
	      grid[x][y] = org;
	   }
	}

	/** Displays the world grid in ASCII.text with Organisms denoted by
	 *  their display character, getPrintableChar()
	 */
	public void displayWorld() {
	   System.out.println("\n\n*****************************************\n");
	   for (int i = 0; i < World.WORLDSIZE; i++)	{
	      System.out.println();
	      for (int j = 0; j < World.WORLDSIZE; j++)	{
	         if (grid[i][j]==null) {
	            System.out.print(".");
	         } else {
	            // X for Doodle, o for Ant
	            System.out.print(grid[i][j].getPrintableChar());
	         }
	      }
	   }
	   System.out.println();
	}

	/** This is the main routine that simulates one turn in the world.
	 * First, a flag for each organism is used to indicate if it has moved.
	 * This is because we iterate through the grid starting from the top
	 * looking for an organism to move. If one moves down, we don't want
	 * to move it again when we reach it.
	 * First move doodlebugs, then ants, and if they are still alive then
	 * we breed them.  If any Organism is starving at the end of this turn,
	 * it will be killed off.
	 */
	public void SimulateOneStep()	{
		 // For this new turn, reset all organisms to not moved
	   for (int i = 0; i < World.WORLDSIZE; i++) {
	      for (int j = 0; j < World.WORLDSIZE; j++)	{
	         if (grid[i][j] != null) {
	            grid[i][j].setMoved(false);
	         }
	      }
	   }

		 // Move all Doodlebugs, checks that the Doodlebug found has not
		 //  moved yet this turn (since it can move right and down to a
		 //  new grid position 
	   for (int i = 0; i < World.WORLDSIZE; i++) {
	      for (int j = 0; j < World.WORLDSIZE; j++) {
	         if ((grid[i][j] != null) && 
	               (grid[i][j] instanceof Doodlebug) &&
	               (!grid[i][j].getMoved()) ) {
	            // NOTE: setMoved() is used to track whether an Organism
	            //  has "attempted" to move so it is not moved again,
	            //  there is no other purpose for this variable
	            grid[i][j].setMoved(true);
	         }
	      }
	   }
		
		 // Move all Ants - similar to above Doodlebug move loop
	   for (int i = 0; i < World.WORLDSIZE; i++) {
	      for (int j = 0; j < World.WORLDSIZE; j++) {
	    	  if ((grid[i][j] != null) && 
		               (grid[i][j] instanceof Ant) &&
		               (!grid[i][j].getMoved()) ) {
		            // NOTE: setMoved() is used to track whether an Organism
		            //  has "attempted" to move so it is not moved again,
		            //  there is no other purpose for this variable
		            grid[i][j].setMoved(true);
		      }
	      }
	   }
		
		// Kill off any starving Organisms
		for (int i = 0; i < World.WORLDSIZE; i++) {
			for (int j = 0; j < World.WORLDSIZE; j++) {
			   if (grid[i][j] != null)
				   grid[i][j].starve();
			   grid[i][j] = null;
			}
		}	
		
		// Breed any Organisms that have moved and have enough breedTicks
		//  to move.
		for (int i = 0; i < World.WORLDSIZE; i++) {
		   for (int j = 0; j < World.WORLDSIZE; j++) {
			   if (grid[i][j] != null && grid[i][j].move()) {
				   grid[i][j].breed();
			   }   
		   }
		}
	}
	
	public boolean isAnt(int x, int y) {
		boolean emptySpot = false;
		if(grid[x][y] instanceof Ant)
			emptySpot = true;
		    return emptySpot;
	}
	
	public boolean isNull(int x, int y) {
		boolean emptySpot = false;
			if(grid[x][y] == null)
				emptySpot = true;
		     return emptySpot;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: getting 'void' type not allowed here error

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 04:02 PM

From line 134, what would this term, grid[i][j].move(), in the if statement evaluate to?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 04:16 PM

What does

Organism.move() method returns ?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5835
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 04:27 PM

The only way I can figure you'd get that is if the move method does not return a boolean.

Also, you have a number of methods, like getAt and isNull, that seem to want you to abstract away from calling grid[x][y] directly. Why not use those?
Was This Post Helpful? 0
  • +
  • -

#5 javojava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-August 11

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 04:39 PM

Hi guys thanks for taking the time. I realized I only want to breed organisms that have moved, since breeding places new organisms on the map I don't want to try and breed those. Changed line 134 to if ((grid[i][j] != null) && (grid[i][j].moved)) { grid[i][j].breed(); }

Now the program compiles and runs the first time (placing the ants and doodlebugs), but once I initiate the second run, I get no data. I'm looking at my for loops since I am really not that good at coding those....
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5835
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 04:46 PM

I felt the need to compulsively organize this.

Stop talking to grid[][] if you have methods to do the work for you.

public class World {
	// I had to fake it
	class Organism {
		public boolean move() { return false; }
		public char getPrintableChar() { return 'X'; }
		public void setMoved(boolean value) { }
		public boolean getMoved() { return false; }
		public void breed() { }
		public void starve() { }
	}
	
	class Doodlebug extends Organism { }
	class Ant extends Organism { }
	
	public static final int WORLDSIZE = 20;
	
	private Organism[][] grid;

	public World() {
		grid = new Organism[WORLDSIZE][WORLDSIZE];
	}
	
	private boolean isInBounds(int x, int y) {
		return ((x >= 0) && (x < World.WORLDSIZE) && 
				(y >= 0) && (y < World.WORLDSIZE));
	}
	
	public Organism getAt(int x, int y) {
		return isInBounds(x,y) ? grid[x][y] : null;
	}

	public void setAt(int x, int y, Organism org) {
		if (isInBounds(x,y)) { grid[x][y] = org; }
	}

	public void displayWorld() {
		System.out.println("\n\n*****************************************\n");
		for (int x = 0; x < World.WORLDSIZE; x++) {
			System.out.println();
			for (int y = 0; y < World.WORLDSIZE; y++) {
				Organism org = getAt(x,y);
				if (org==null) {
					System.out.print(".");
				} else {
					System.out.print(org.getPrintableChar());
				}
			}
		}
		System.out.println();
	}

	public void SimulateOneStep(){
		for (int x = 0; x < World.WORLDSIZE; x++) {
			for (int y = 0; y < World.WORLDSIZE; y++) {
				Organism org = getAt(x,y);
				if (org != null) { org.setMoved(false); }
			}
		}

		// it doesn't matter if it's an Ant or Doodlebug
		for (int x = 0; x < World.WORLDSIZE; x++) {
			for (int y = 0; y < World.WORLDSIZE; y++) {
				Organism org = getAt(x,y);
				if ((org != null) && !org.getMoved() ) {
					org.setMoved(true);
				}
			}
		}
		
		// Kill them all, no clear reason why
		for (int x = 0; x < World.WORLDSIZE; x++) {
			for (int y = 0; y < World.WORLDSIZE; y++) {
				Organism org = getAt(x,y);
				if (org != null) {
					org.starve();
					setAt(x,y,null);
				}
			}
		}
		
		// They're all going to be null, because of above
		// so we're kind of done
	}
	
	public boolean isAnt(int x, int y) {
		Organism org = getAt(x,y);
		return org!=null && org instanceof Ant;
	}
	
	public boolean isNull(int x, int y) { return getAt(x,y) == null; }

}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 javojava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-August 11

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 06:36 PM

Hey, after you decided to clean my code, I realized that the reason the program runs initially, but it doesn't display anymore ants or doodlebugs after you initiate another run is lines 71 through 79 of your code. I am not trying to kill them all but that is my problem, I am trying to breed organisms that have moved, not kill them all...

Thanks
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7765
  • View blog
  • Posts: 13,129
  • Joined: 19-March 11

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 06:56 PM

Quote

I am trying to breed organisms that have moved, not kill them all...



In that case, this code is not correct.

	// Kill off any starving Organisms
		for (int i = 0; i < World.WORLDSIZE; i++) {  
			for (int j = 0; j < World.WORLDSIZE; j++) {  // iterate through the world
			   if (grid[i][j] != null)        // find bugs
				   grid[i][j].starve();   //  and kill them
			   grid[i][j] = null;             // and then make them gone.
			}
		}



Perhaps that clarifies things...

This post has been edited by jon.kiparsky: 05 September 2011 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 javojava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-August 11

Re: getting 'void' type not allowed here error

Posted 05 September 2011 - 07:05 PM

It did, compiled the file and the output data was somewhat correct. It was killing all the doodlebugs and moving the ants...

I edited this piece of code

 // Move all Ants - similar to above Doodlebug move loop
	   for (int i = 0; i < World.WORLDSIZE; i++) {
	      for (int j = 0; j < World.WORLDSIZE; j++) {
	    	  if ((grid[i][j] != null) && 
		               (grid[i][j] instanceof Ant) &&
		               (!grid[i][j].getMoved()) ) {
		            // NOTE: setMoved() is used to track whether an Organism
		            //  has "attempted" to move so it is not moved again,
		            //  there is no other purpose for this variable
		            grid[i][j].setMoved(false);
		            grid[i][j].move();
		      }
	      }
	   }



And it seems to be working correctly.

Thank you all for the assistance.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5835
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: getting 'void' type not allowed here error

Posted 06 September 2011 - 04:14 AM

Glad you got it to work. You're still talking directly to grid, when you have those nice methods, which is unfortunate.

Also, it would seem that your methods have some understanding of the world, with things like move, etc. This is also unfortunate; it means they'd have to have a reference to the world instance they inhabit? If there is a starve, then is their food somewhere? If there is a breed, then then most be some kind of localized conditon.

Sorry, this looks amusing but the rules are unclear and they shouldn't be if the code is.

Nevermind, found the assignment ( http://www.brainmass...design/416303/b ). Doesn't look like you've done much work at all here. It does explain why you didn't change your code, you don't want to touch what you've already been given, which is pretty much all of it.

It's kind of a poor design, to be honest.

Good luck, then.
Was This Post Helpful? 1
  • +
  • -

#11 javojava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-August 11

Re: getting 'void' type not allowed here error

Posted 06 September 2011 - 08:14 AM

As explained on the first post:

Quote

Line 134...that part of the code was provided by the instructor, and I am lost here. Any help would be greatly appreciated.


I am just learning Java and believe me, my brain is not wired to understand this the first time around...

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1