[FIXED] How to print a 'state' from an array

Hello!

I’m currently builing a game where you have to construct a building. This building has to be contructed by using different types of bricks with different colours etc. Since it’s only the start of the design-phase, I am currently trying to let everything work with only text.

Right now I’m trying to print an array of all available building-bricks. However, when I try print this, the code is printed like [mygame.Bricks@6676284a, mygame.Bricks@100d1e93]. I’ve heard about Arrays.toString, but because this is an object, I cannot seem to find a way to get the right command.

Even better, I would like to print a specific variable that is applied to each of there bricks.

Summarizing: I would like to know if there is a way to print a single characteristic from an array with objects with multiple characteristics.

Here is the code that I’m using (I’m pretty new to Java and jME3 so there might be some horrible code right here)

public class Bricks {
    protected Integer   ID;
    protected String    name;
    protected String    colour;
    protected Integer   length;
    protected Integer   width;
    
    protected Bricks(Integer brickID, String brickName, String brickColour, 
            Integer brickLength, Integer brickWidth) {
    ID     = brickID;
    name   = brickName;
    colour = brickColour;
    length = brickLength;
    width  = brickWidth;
    }
    
    protected Integer getID() {
        return ID;
    }
    protected String getName() {
        return name;
    }
    
    protected String getColour() {
        return colour;
    }
    
    protected Integer getLength() {
        return length;
    }
    
    protected Integer getWidth() {
        return width;
    }
    
           
    protected static void initBricks() {
        ArrayList<Bricks> AvailableBricks = new ArrayList<>();
        Bricks redBrick1 = new Bricks(1,"Red brick with 4x2 dimensions","Red",4,2);
        AvailableBricks.add(redBrick1);
        Bricks redBrick2 = new Bricks(2,"Red brick with 3x2 dimensions","Red",3,2);
        AvailableBricks.add(redBrick2);
        // This needs a lot of more bricks, but I haven't added them yet.
        
        
        // This gives the [mygame.Bricks@6676284a, mygame.Bricks@100d1e93] code,
        // but I want it to print the String name of each 'Bricks'.
        // Like: [Red brick with 4x2 dimensions, Red brick with 3x2 dimensions]
        Bricks[] availableBricks = AvailableBricks.toArray(new Bricks[AvailableBricks.size()]);
        System.out.println(availableBricks);
    }
}

Is this possible?
Thanks in advance!

When you call System.out.println(yourBrickObject)then the toString()method for this class is called, this is very basic Java stuff though. To apply a custom output you’d need to override this method in your Brick class.

In regards of the different types, you could define all possible BrickTypes by using an enum for example and let the Brick class have this as an attribute. Instead you could also use inheritance.

Maybe you should make yourself familiar with Java first before you start developing a game. You’ll see that you will learn pretty fast but you really should take yourself the time you need.

I have made myself familiar with Java, but it had to be in a fairly quick time-frame (this is for my thesis and I only have 10 weeks). Because of this, I might’ve forgot some stuff like enums and when to use them.

However, I don’t want to print one single object, but I want to print the same characteristic from all items in the availableBricks array.

I have found a solution though, so I think no more help is needed:

// AvailableBricks is created earlier in the class
protected static void initBricks() {
        Bricks redBrick1 = new Bricks(1,"Red brick with 4x2 dimensions","Red",4,2);
        AvailableBricks.add(redBrick1);
        Bricks redBrick2 = new Bricks(2,"Red brick with 3x2 dimensions","Red",3,2);
        AvailableBricks.add(redBrick2);
    }
    
    protected static void showBricks() {
        Bricks[] availableBricks = AvailableBricks.toArray(new Bricks[AvailableBricks.size()]);
        for (int i = 0; i < AvailableBricks.size(); i++) {
            System.out.println(availableBricks[i].getName());
        }
    }

The output now is

Red brick with 4x2 dimensions
Red brick with 3x2 dimensions

Which was what I tried to get.

Thanks for your help!

You missed a LOT of Java in your crash course, I guess…

Why create an array just to iterate over a list?

        for (int i = 0; i &lt; AvailableBricks.size(); i++) {
            System.out.println(AvailableBricks.get(i).getName());
        }

Or even:

        for( Bricks b : AvailableBricks ) {
            System.out.println(b.getName());
        }

The former could have easily been found in the javadocs.

If you plan to develop in Java and don’t have the javadoc already open or one click away then just quit now, I guess.

If you don’t keep the javadoc open and have some links to some basic Java tutorials handy, it is going to be a very long 10 weeks for you.

I’ve found the javadoc pretty quick after writing my previous post yes, and it’s all good now.