Printing Issues

/** Print the table. */

publicvoid print()

{

System.out.println("");

System.out.println("P = Piles : B = Blocks");

System.out.println("");

System.out.println("Pile : Block");

for (int pile = 0; pile < piles.length; pile++){

System.out.println("Pile " + pile +": " + piles[pile]);

}

}

this code prints this

P = Piles : B = Blocks

Pile : Block

Pile 0: Pile@16a9b9c

Pile 1: Pile@e2291

Pile 2: Pile@1ff92f5

Pile 3: Pile@9505f

Pile 4: Pile@17ba38f

though what i need is something that looks like this

Pile : Block

Pile 0: 0

Pile 1: 1

Pile 2: 2

Pile 3: 3

Pile 4: 4

the idea is so i can take one block from one pile and stack it on another block!! but i cant do that unitll i get rid of the "Pile@17ba38f"

it seems to me that it print the pile ok but not the ArrayList of blocks any ideas

[1385 byte] By [JohnnyMaca] at [2007-10-3 3:35:43]
# 1

Your Pile class has to have a toString() method where it describes itself.class Pile {

private String name;

private int size;

// other stuff

public String toString() {

return name + " size=" + size;

}

}

You can put whatever you like in the toString() method - this is just an

example. If you don't define your own toString() then java uses the default which

is to print out a sort of "address" that the pile has.

pbrockway2a at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 2

public class Table

{

private int NPILES;// the number of piles

private Pile[] piles; // the array of piles

/** Create a table and initialise the array piles.

* For every pile created create a block

*/

public Table(int npiles)

{

// Initialise NPILES

NPILES = npiles;

// Create the array of size NPILES

piles = new Pile[NPILES];

for (int i = 0; i < NPILES; i++) {

Pile pile = new Pile();

Block block = new Block(i);

piles[i] = new Pile();

}

}

thats my class the block is an integer i dont understanfd what u mean below!! sorry im a bit new to java

JohnnyMaca at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 3
The problem is in the Pile class, not the Table class. It will (probably) be in afile called Pile.java, and probably not in Table.java.Once you have found the Pile class add a toString() method like the one in myexample.
pbrockway2a at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 4

ok i added that to the pile class i thought id just show u what it looks like to see if ive done anthing wrong!!

cheers

import java.util.ArrayList;

import java.util.Iterator;

/**

* Write a description of class Pile here.

*

* @author (your name)

* @version (a version number or a date)

*/

public class Pile

{

private ArrayList<Block> blocks;

private String name;

private int size;

/**

* Creates a New pile and initialises its state

* Creates a block with a pile

*/

public Pile()

{

blocks = new ArrayList<Block>();

}

/**

* Add block to a pile

*/

public void addBlock(Block block)

{

blocks.add(block);

}

/**

* To string meathod

*/

public String toString() {

return name + " size=" + size;

}

/**

* Remove a block from a pile

*/

public void removeBlock(int blockNumber)

{

if (blockNumber <0){

//this is not a valid block number, so do nothing

}

else if (blockNumber < pileSize()){

//This is a valid block number, so we can remove it

blocks.remove(blockNumber);

}

else{

//this is not a valid block number so do nothing

}

}

/**

* Return the size of a pile

*/

public int pileSize()

{

return blocks.size();

}

/** Returns the position of a block with number n if present, else -1. */

public int search(int n) {

int pos = 0;

boolean found = false;

while (! found && pos < blocks.size()) {

if (blocks.get(pos).getNumber() == n)

found = true;

else

pos = pos + 1;

}

if (found)

return pos;

else

return -1;

}

/**

* Print a pile

*/

public void DisplayPile()

{

for (Block block : blocks){

System.out.println(block.getNumber());

}

}

}

JohnnyMaca at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 5

It was meant as an example!

Remove the name and size declarations. And replace toString() withpublic String toString()

{

StringBuilder buf = new StringBuilder();

for (Block block : blocks){

buf.append(block.getNumber()).append(" ");

}

return buf.toString();

}

As you can see the code is similar to what you are already using to describe

the Pile. (The difference is that prints the contents in one line)

edit: sorry about changing this after posting it!

pbrockway2a at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 6
missin return statementMessage was edited by: JohnnyMac
JohnnyMaca at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 7
>Now it prints the size is nullwhich became> missin return statementSee my previous post - which I think I've finally got right.
pbrockway2a at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 8

Yeah i got that and changed it but my table prints

but its still not printing my blocks

>> it would need to get the block index from the arraylist and print it in the pile!! is that right?

Message was edited by:

JohnnyMac

Message was edited by:

JohnnyMac

JohnnyMaca at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 9

> but its still not printing my blocks

What do you mean by "not printing my blocks"? Bear in mind that I haven't

seen your Block class.

What my suggestion does is print the result of calling the getNumber()

method of each block in the Pile. If you something other than the this to

be shown, then add the code to Pile's toString() to add whatever it is

that you want to have printed.

Or you could write a toString() method for the Block class and use that

within the Pile's toString() method.

> it would need to get the block index from the arraylist and print it in the

> pile!! is that right?

The iterator will return process each Block in the standard order (starting

from index 0 and increasing), so I don't you have to print the index along

with the block's description. But, again, that is up to you.

pbrockway2a at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 10
Now im back at square one it wont add my block to the pile when i print any ideas
JohnnyMaca at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...
# 11

Yeah im not tryin to print the blocks description im tryin to print so when the table class initialises a pile say

pile 0 :

it creates block 0 as well and prints it as so

pile 0 : block 0 or just

0:0

1:1

2:2

3:3

4:4

an so on, i dont think u need to enter a block descrpiption everything is done in the table class

> but its still not printing my blocks

>>What my suggestion does is print the result of calling the getNumber()

method of each block in the Pile. If you something other than the this to

be shown, then add the code to Pile's toString() to add whatever it is

that you want to have printed.

>>so what i should do is create a meathod that gets the index number of the block ArrayList and print it?

then call the meathod in ur suggestion. do u think that will work

JohnnyMac

JohnnyMaca at 2007-7-14 21:30:30 > top of Java-index,Java Essentials,Java Programming...