0

I'm trying to convert a row from a matrix of Char's to a String and all I've been able to get is a bunch of memory locations. I was hoping someone can lend me a hand. Here's what I've got:

for(int c=0; c<tempBoard.length; c++){
        		String charArray = " ";
        		for(int v=0; v<tempBoard.length; v++){
        			board[c][v] = tempBoard[c][v];
        			charArray = tempBoard[c].toString();
        		}
        		System.out.println(charArray);
        	}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Encrypted
0

Calling a String 'charArray' is pretty confusing to say the least. Anyway, here is an example to clear things up. This puts the first rows of chars into the variable temp.

char[][] chars = new char[10][10];
		
		//Put some stuff in the 2d array
		for (int i = 0; i < 10; i++)
			for (int j = 0; j < 10; j++)
				chars[i][j] = (char) (i*j);
		
		// 0 is the row, i is the column
		String temp = "";
		for (int i = 0; i < 10; i++) temp+=chars[0][i];
0

Thanks, that should do it.. not sure why I didn't think of that. About the variable.. yeh I know, I'm too used to coding in C and that happened to be the first thing that crossed my mind.

Calling a String 'charArray' is pretty confusing to say the least. Anyway, here is an example to clear things up. This puts the first rows of chars into the variable temp.

char[][] chars = new char[10][10];
		
		//Put some stuff in the 2d array
		for (int i = 0; i < 10; i++)
			for (int j = 0; j < 10; j++)
				chars[i][j] = (char) (i*j);
		
		// 0 is the row, i is the column
		String temp = "";
		for (int i = 0; i < 10; i++) temp+=chars[0][i];
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.