# Multidimensional char array

• 04-11-2011, 01:15 PM
leepikamukharji
Multidimensional char array
Hi,
I am facing problem in displaying character matrix in arrays. I have made a matrix for numbers and the coding for that is as below :-

class TwoDArray
{
public static void main(String ar[])
{
int[][] twod=new int[5][5];
for(int i=0;i<twod.length;i++)
{
for(int j=0;j<twod[i].length;j++)
{
twod[i][j]=i;
System.out.print(+twod[i][j]);
}
System.out.println("");
}

}
}
Output :-

00000
11111
22222
33333
44444

but when I am trying for character matrix, so many errors :(
My coding for char array is as follows:

class CharArray
{
public static void main(String ar[])
{

char ca=new char[2][2];
ca[0][0]='a';
ca[0][1]='b';
ca[1][0]='c';
ca[1][1]='d';

for(int i=0;i<ca.length;i++)
{
for(int j=0;j<ca[i].length;j++)
{
ca[i][j]=(char)i;
System.out.println(ca[i][j]);
}

System.out.println("");
}
}
}

Kindly help me in this.
• 04-11-2011, 01:37 PM
baf06
• 04-11-2011, 01:40 PM
baf06
an one more thing, notice the difference between these two, when they are supposedly have to perform the same task

Quote:

int[][] twod=new int[5][5];
Quote:

char ca=new char[2][2];
• 04-11-2011, 02:08 PM
leepikamukharji
Hey thanks for the help. It was silly mistake by me. when I am running the program

class CharArray
{
public static void main(String ar[])
{

char[][] ca=new char[2][2];
ca[0][0]='a';
ca[0][1]='b';
ca[1][0]='c';
ca[1][1]='d';

for(int i=0;i<ca.length;i++)
{
for(int j=0;j<ca[i].length;j++)
{ System.out.print(ca[i][j]);
}

System.out.println("");
}
}
}

proper output is there as
ab
cd

again a question

in this program I have already given value to each array and if I want to make a generalised program so that I give any dimension of the matrix it will print as per it such as char[][] ca=new char[5][5];

It should print

a b c d e
f g h i j
k l m n o
p q r s t
u v w x y

or print
a a a a a
b b b b b
c c c c c
d d d d d
e e e e e

how can we do it.. I was trying as below but getting default symbols as below..

@@@@@
*****
&&&&&

the program is as below :-

class CharArray
{
public static void main(String ar[])
{

char[][] ca=new char[4][4];

for(int i=0;i<ca.length;i++)
{
for(int j=0;j<ca[i].length;j++)
{
ca[i][j]=(char)i;
System.out.print(ca[i][j]);
}

System.out.println("");
}
}
}
• 04-11-2011, 02:47 PM
MaceMan
A nice way to get the array elements to print, other then the for loop, is to use the for-each loop. It is simpler and easyer to understand.

Syntax: for (Array_Base_Type Variable : Array_Name)
Statement

For example:
double[] a = new double [10]
<some code to fill the array a>
for (double element : a)
System.out.println(element)

You can read the for (double element : a) as,
"for each element in a, do the following."
Just thought I would tell you that, in case you don't want to use the for loop.
I find it easyer to understand the logic using the for-each.
• 04-12-2011, 03:41 PM
leepikamukharji
Hi, I didnt get u wat u want to say.. I have already provided the code and want to know that where exactly is the error as I m getting default symbols instead of characters. Kindly suggest me.
• 04-12-2011, 03:47 PM
baf06
Hi there, in your code you didn't specify what are the elements in your char array, unlike in the first case, so surely it will print some symbols depending on their ascii number.
My advice to you is that before going into the loop that will print the array you have to go through a for-loop to fill it up depending on what you want it to print
• 04-12-2011, 04:09 PM
Tolls
Quote:

Originally Posted by leepikamukharji
Hi, I didnt get u wat u want to say.. I have already provided the code and want to know that where exactly is the error as I m getting default symbols instead of characters. Kindly suggest me.

That code you posted up there doesn't produce the output you show above.
It produces the first 4 ascii characters, which are NUL, SOH (start of header), STX (start of text) and ETX (end of text). Which are essentially blank.

What did you expect
Code:

`ca[i][j]=(char)i;`
to do?

And (when posting code) can you please use code tags.
• 04-13-2011, 07:58 AM
leepikamukharji
Hey u r right that the symbols were the same as u described. I just meant to say that it displays symbol..

the code
ca[i][j]=(char)i;

I wrote it to typecast the integer to char value but it didn't work. Can u suggest me what exactly I need to write instead.

for an array having huge number of elements.. I cannot specify each element so i need to provide a generalized statement which will print the matrix of any dimension which I provide. Kindly specify the statement which I need to add in that.
• 04-13-2011, 08:25 AM
Tolls
I have no idea what you want.
• 04-13-2011, 08:30 AM
pbrockway2
Quote:

I wrote it to typecast the integer to char value but it didn't work

But it did work: you asked for and got the symbols you saw. What you need to do is figure out and tell us what characters you meant to assign.

Quote:

I cannot specify each element so i need to provide a generalized statement which will print the matrix of any dimension

But you must specify each element: otherwise how can the computer decide what values to use? You can use loops to do this so that you don't need one line of code for each element you specify. But you must specify them all unless you want the default values (zero).

(by the way you are talking about the size of the matrix not the dimension)
• 04-13-2011, 09:37 AM
leepikamukharji
I just want a simplest program which displays multidimensional char array or matrix of char array...
• 04-13-2011, 09:51 AM
Tolls
Which has nothing to do with your problem about char[i][j] = (char)i;

What are you trying to do there?
• 04-13-2011, 09:55 AM
leepikamukharji
I am confused, kindly provide me a simple program for that.
• 04-13-2011, 10:12 AM
Tolls
Code:

```for(int i=0;i<ca.length;i++) {     for(int j=0;j<ca[i].length;j++)     {         System.out.print(ca[i][j]);     }     System.out.println(""); }```
Does what you say in post #12.
So consequently I have no idea what it is you are requesting.
• 04-13-2011, 12:47 PM
leepikamukharji
I tried again with the code..now its printing all the spaces means totally blank.
My program is as follows :-

class CharArray
{
public static void main(String ar[])
{

char[][] ca=new char[4][4];

for(int i=0;i<ca.length;i++)
{
for(int j=0;j<ca[i].length;j++)
{

System.out.print(ca[i][j]);
}

System.out.println("");
}
}
}

Please tell me how will I get
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y

as output
• 04-13-2011, 01:02 PM
dswastik
Your last code does not store the values in the array.

Code:

```public static void main(String arg[]){                 char ch='a';                 char [][] ca=new char[5][5];                 for(int x=0;x<5;x++){                         for(int y=0;y<5;y++){                                 ca[x][y]=ch;                                 ch++;                         }                 }                 for(int x=0;x<5;x++){                         for(int y=0;y<5;y++){                                 System.out.print(ca[x][y]+" ");                         }                         System.out.println();                 }         }```
• 04-13-2011, 01:16 PM
leepikamukharji
Thanks a lot :)

I m really really thankful to you .... Atlast I got answer to my question.
• 04-13-2011, 01:18 PM
dswastik
You are welcome.