Greetings to the community!

I am trying to print a two-dimensional character array and it is printing out ASCII values instead of the actual characters. I think the solution is simple but I can't figure it out.

The following is the code snippet and the function that I am using to print out the character array:

char e = 'F';
char[][] a = new char[5][5];
	
a[2][3] = e;
	
prA(a);


private void prA(char[][] a){		
    for(int i=0; i<a.length; i++){			
        for(int j=0; j<a[i].length; j++){
	    System.out.print(a[i][j] + ' ');	
	}
	System.out.println();
    }
}

The following is the output that I am getting:

3232323232
3232323232
32323210232
3232323232
3232323232

I was expecting 'F' to show up in the output but instead I am getting a bunch of numbers. They seem to be ASCII codes for the characters but that is not what I wanted. Should I be using another function instead of System.out.println? I want to print out to the console (which System.out.println() does).

Help!

Any and all help will be greatly appreciated!

Jack Clawson

Recommended Answers

All 5 Replies

System.out.print(a[i][j] + ' ');

change to

System.out.print(a[i][j] + " ");

and see.

Sorry but that does not work at all. I tried to do this before and all I got was empty space. The output was empty spaces.

System.out.println(a[i][j]);

because char + char will result in integer

Sorry but that does not work at all. I tried to do this before and all I got was empty space. The output was empty spaces.

karim.naggar is right. and the solution i provided does work. ive compiled and checked.

and try this. it'd be easier this way to check i suppose.

System.out.println("value at " +i+ "," + j + " is: " + a[i][j]);

//value at 2,3 is: F

Thanks kekkaishi and karim.naggar for your help. You guys were right: once I did that it worked on the console. Apparently for some reason, working in Eclipse Ganymede it gave me these issues (weird!). But in the end the following change works in Eclipse Ganymede console as well:

if (b[i][j] != '\0'){
					System.out.print(b[i][j] + " ");
				}
				else{
					System.out.print('0' + " ");
				}

Hope this information helps someone in the future!

Once again, thanks for the help!

Jake Clawson

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.