0

So i am having problems with my code here. What I am basically trying to do is assigning a value to each BC[0] and adding 1 to BC01[0] and 2 to BC02[0] doing that for each boat. (trying to make battleships (advanced))

but all i am able to print out is

[[I@7852e922

Anyways, here is my code

public class dogs {
    public static int BC00 [] = {0,0};
    public static int BC01 [] = {BC00[0],BC00[1]};
    public static int BC02 [] = {BC00[0],BC00[1]};
    public static int boat0[][] = {BC00, BC01, BC02}; //location cells boat[0][0]
    public static int BC10 [] = {BC01[0], BC01[1]};
    public static int BC11 [] = {BC01[0], BC01[1]};
    public static int BC12 [] = {BC01[0], BC01[1]};
    public static int boat1 [][] = {BC10, BC11, BC12};
    public static int BC20 [] = {BC02[0], BC02[1]};
    public static int BC21 [] = {BC02[0], BC02[1]};
    public static int BC22 [] = {BC02[0], BC02[1]};
    public static int boat2 [][] = {BC20, BC21, BC22};
    public int NH; // Number of hits
    public static int allBoats [][][] = {boat0, boat1, boat2};
    public static int rand;
    public static int n = 0;
    public static void main(String[] args) {

        for (int x = 0; x <= 2; x++) {
            Random gen = new Random();

            rand = gen.nextInt(6) + 1;
            allBoats[x][0][0] = (int) rand;
            while(n <= 2) { //give boatCells locations

                allBoats[n][1][1] = allBoats[n][1][1] + 1;
                allBoats[n][2][1] = allBoats[n][2][1] + 2;
                n++;
            } // end of while loop
        } // end of For loop
        System.out.println(boat0);
    } // end of public static void main(String[] args)
} // end of dogs class
5
  • You should probably tag this with the specific programming language you are using. Commented Jun 5, 2014 at 19:03
  • 1
    possible duplicate of Why does the toString method in java not seem to work? Commented Jun 5, 2014 at 19:08
  • take a look at Java Variables Naming Conventions Commented Jun 5, 2014 at 19:12
  • Since n is a static field, the first iteration of your for loop is the only one where your while will be entered. Also, you don't need to keep re-instantiating Random (and in fact that's a really bad idea). You don't need a temporary int (or a cast) for gen.nextInt() either. Commented Jun 5, 2014 at 19:14
  • tostring() is not for creating "user friendly" output (as mentioned in the answer below). But I have some other comments for your code: All you variables are pulic (that is bad in cases of data-encapsulation) You write Comments at the end of all blocks (this is also bad (in my opinion), because this comments will become wrong if you forget to change one vblock) Commented Jun 5, 2014 at 19:19

1 Answer 1

1

Use

System.out.println(Arrays.deepToString(boat0));

instead of

System.out.println(boat0);

And as @ChristianKuetbach has suggested, it is important to understand that in Java, toString()-Mothod often does not mean "A mothod to generate a user-readable String". Most classes prints ClassName@HashCode.

Sign up to request clarification or add additional context in comments.

1 Comment

You should explain, that in Java a toString()--Mothod does not mean "A mothod to generate a user-readable String". The most classes prints ClassName@HasCode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.