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);
        	}

Recommended Answers

All 2 Replies

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];

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];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.