A Question On Returns and For Loops

I'm afraid I'm quite new at this, so bear with me.

I'm attempting to set up a method to make a print statement via a for loop:

public void printOut()

{

for(int row = 1; row < size - 1; row++){

for(int col = 1; col < size - 1; col++){

System.out.println(array[row][col]);

}

}

}

And then to take this information and override the toString() method, so that it will successfully return with all the values in the two-dimensional array, e.g.:

board.toString()

Every time I access it in a separate class, it prints out absolutely nothing. I've been reading up and busting my head for a while on this. Is anyone curious as to know how to perform repetitive actions like printing the elements of the array through methods like this? It seems obvious, and yet not.

Thanks,

WM

[868 byte] By [WillMacka] at [2007-11-27 7:56:46]
# 1

> I'm afraid I'm quite new at this, so bear with me.

>

> I'm attempting to set up a method to make a print

> statement via a for loop:

>

> public void printOut()

> {

> for(int row = 1; row < size - 1; row++){

>for(int col = 1; col < size - 1; col++){

>System.out.println(array[row][col]);

>}

> }

>

>

> And then to take this information and override the

> toString() method, so that it will successfully

> return with all the values in the two-dimensional

> array, e.g.:

>

> board.toString()

>

> Every time I access it in a separate class, it prints

> out absolutely nothing. I've been reading up and

> busting my head for a while on this. Is anyone

> curious as to know how to perform repetitive actions

> like printing the elements of the array through

> methods like this? It seems obvious, and yet not.

>

If you want to override the toString, you need to have output that is a String, not void.

petes1234a at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 2

I believe I did this:

public String toString{

return printOut()

}

Along with public void printOut().

That's the issue right now. :(

Even if I do public String printOut(), it won't let me put the return in the for loop, thus I can't print out every iteration of the array.

WillMacka at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 3
printOut returns void, i.e., nothing. So what value is toString supposed to be able to pull out of that to return?
jverda at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 4

You probably want something like this.

public String toString()

{

String info = "";

for(int row = 1; row < size - 1; row++){

for(int col = 1; col < size - 1; col++){

info += array[row][col];

}

}

return info;

}

You need to take everything that you wanted to print out, put it into a String, then return out that String.

CaptainMorgan08a at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 5

> Even if I do public String printOut(), it won't let

> me put the return in the for loop, thus I can't print

> out every iteration of the array.

What do you mean? Be specific. Show code and the exact, complete error message.

When you post code, please use[code] and [/code] tags as described in http://forum.java.sun.com/help.jspa?sec=formattingFormatting tips on the message entry page. It makes it much easier to read.

jverda at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 6

These ideas are helpful, thank you.

This is what I was looking for:

public String printOut()

{

String string;

for(int row = 1; row < size - 1; row++){

for(int col = 1; col < size - 1; col++){

string = array[row][col];

}

}

return string;

}

//-

public String toString()

{

return printOut();

}

There are no errors (aside from my logic): it prints out nothing. It will leave a blank space.

Message was edited by:

WillMack

WillMacka at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 7

> These ideas are helpful, thank you.

>

> This is what I was looking for:

>

> public String printOut()

> {

> String string;

> for(int row = 1; row < size - 1; row++){

> for(int col = 1; col < size - 1; col++){

>string = array[row][col];

> }

What type is the array? If it's not String[][], then you can't do string = array[row][col]. The simplest thing in that case would be string = String.valueOf(array[row][col].

Also, you're assigning one value to string, then another, another, another, each time throwing away the previously assigned value, until finally you just assign the last array element and return that. You should use StringBuilder's append method to add pieces to the string.

Finally, why even have the printOut method? Why not just put all that stuff inside toString? printOut's not even a good name, as it doesn't actually print anything.

jverda at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 8

It is an array of strings.

I am simulating a game board of Minesweeper, so I need to print out certain symbols and spaces to match with flags, queries, mines, etc. So I'd like the screen to print out every time a new action is made. It's archaic and stuffy, but I'm learning on my way.

Thanks.

WillMacka at 2007-7-12 19:38:24 > top of Java-index,Java Essentials,New To Java...
# 9

> It is an array of strings.

Array of array of strings, actually.

>

> I am simulating a game board of Minesweeper, so I

> need to print out certain symbols and spaces to match

> with flags, queries, mines, etc. So I'd like the

> screen to print out every time a new action is made.

> It's archaic and stuffy, but I'm learning on my

> way.

Okay, so, if you're trying to build a single string that represents the board, you'll have to build it up piece by piece with StringBuilder's append method, and then after each row, add a line break.

jverda at 2007-7-12 19:38:27 > top of Java-index,Java Essentials,New To Java...
# 10
All right, that sounds sensible.Thank you.
WillMacka at 2007-7-12 19:38:29 > top of Java-index,Java Essentials,New To Java...
# 11

Unfortunately, another newbie inquiry.

I have the following code below, establishing three arrays of arrays, each planned to be working off of one another. Unfortunately, I am unable to grab the info inside: when I try to run this code below (assume the rest of the code you don't know about works peachy-keen), I get the following error:

cannot find symbol variable mines

(Line 45, where the "return mines" statement is)

I've done this plenty of times with other code, pulling variables out of the constructor in the same class, and there are no other boolean mines[][] created - does anyone know why this will not work?

public Mineboard()

{

boolean[][] mines = new boolean[row + SIZEOFFSET][col + SIZEOFFSET];

int[][] solution = new int[row + SIZEOFFSET][col+ SIZEOFFSET];

String[][] board = new String[row + SIZEOFFSET][col+ SIZEOFFSET];

for(int a = 1; a <= row; a++)

{

for(int b = 1; b <= col; b++)

{

mines[a][b] = (chance > Math.random()); //ERROR

mineCount++;//Deal with mineCount in order to have it work with the flag system.

}

}

}

public boolean getMines()

{

return mines;

}

Alternatively, this will not work here, where mines[a][.b] has the same error. It can't find symbol variable mines.

public String displayMines()

{

String string = " ";

System.out.println(" | 1 2 3 4 5 6 7 8 9 |");

System.out.println("-");

int down = 0;

for(int a = 1; a <= row; a++)

{

down++;

System.out.print(down + " |");

for(int b = 1; b <= col; b++)

{

if(mines[a][b]) //ERROR

System.out.print(" * ");

else

System.out.print(" . ");

}

System.out.print("|");

System.out.println();

}

System.out.println("-");

return string;

}

WillMacka at 2007-7-12 19:38:29 > top of Java-index,Java Essentials,New To Java...
# 12
It is because you have declared the array ( all of them) inside the constructor. So this the only place it exists, inside the constructor. You need to learn about scope.
floundera at 2007-7-12 19:38:30 > top of Java-index,Java Essentials,New To Java...
# 13
So I am to assume that because it sits locally to the constructor, it won't be accessible outside of that. Okay.
WillMacka at 2007-7-12 19:38:30 > top of Java-index,Java Essentials,New To Java...
# 14
> So I am to assume that because it sits locally to the> constructor, it won't be accessible outside of that.> Okay.Yes, you assume correctly. This is a good example to teach you about scope.
petes1234a at 2007-7-12 19:38:30 > top of Java-index,Java Essentials,New To Java...
# 15
So you can either return the array from the method or have it as a member variable.I haven't looked at your code or design, so I can't say which is more appropriate in this case.
jverda at 2007-7-21 22:24:29 > top of Java-index,Java Essentials,New To Java...