4

I have the following code:

public boolean prontoParaJogar() throws RemoteException {
    int i;
    int j;
    if (this.jogadores==2) {
        this.jogando=1;
        for (i=0;i<3;i++)
            for(j=0;j<3;j++) {
                this.tabuleiro[i][j]=0;
            }

        for (i=0;i<3;i++) {
            System.out.println("Linha "+i+": ");
            System.out.print(this.tabuleiro[i][0]+' ');
            System.out.print(this.tabuleiro[i][1]+' ');
            System.out.print(this.tabuleiro[i][2]);
            System.out.println("");
        }


        return true;
    } else {
        return false;
    }
}

it is printing the following exit:

Linha 0:
32320
Linha 1: 
32320
Linha 2: 
32320
Linha 0: 
32320
Linha 1: 
32320
Linha 2: 
32320

This is not what I expected. It should be the following output:

Linha 0:
0 0 0
Linha 1:
0 0 0
Linha 2:
0 0 0

I can't figure out why its not running as expected.

3
  • 1
    You should show us some more code, otherwise it would be hard to figure out what are you trying to do. Commented May 29, 2011 at 21:11
  • Erm, no ... it's pretty easy to answer the way it is ;) Commented May 29, 2011 at 21:15
  • Just one more observation: It printed the Lines twice the time as expected because the method was part of a RMI service implementation and the method was called by two clients so the code ran twice. Commented May 29, 2011 at 21:35

5 Answers 5

12

that's because you are adding ' ' to your variables, since ' ' is a character with asci code 32, it adds 32 to zero value inside your array and prints 32. you have to write two prints to have the output you formated as you like.

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

2 Comments

@Brian Roach @Jonas Elfström +1 to all who spotted this :) good eyes guys :)
@boro I was first! just by 16 seconds before brian :d
10
this.tabuleiro[i][0]+' '

' ' is the character for whitespace, which has the ascii value 32. Single quotes denote a char value not a String

this.tabuleiro[i][0]+" "

will concatenate a space.

Comments

6

You are adding 0+32, because ' ' is space and that's 32 ASCII, instead of doing a string concatenation. Change to

System.out.print(this.tabuleiro[i][0]+" ");

Comments

1

In your output lines, you are using + ' '. This adds the character ' ' (character value 32) to each entry of tabuleiro. You need to use + " ".

Comments

1

Also don't do this:

for (i=0;i<3;i++)
    for(j=0;j<3;j++) {
        this.tabuleiro[i][j]=0;
    }

but rather this:

for (i=0;i<3;i++) {
    for(j=0;j<3;j++) {
        this.tabuleiro[i][j]=0;
    }
}

or at some point in the future you might do this:

for (i=0;i<3;i++)
    System.out.println("i=" + i);
    for(j=0;j<3;j++) {
        this.tabuleiro[i][j]=0;
    }

and be surprised that the second loop is not being executed three times.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.