Re: Accessing multidimensional char array (228 Views)
Reply
Occasional Advisor
Leepika Mukherjee
Posts: 10
Registered: ‎03-31-2011
Message 1 of 11 (228 Views)

Accessing multidimensional char array

Hi Everyone,

I am facing problem in running the following code of multidimensional char array. The program is properly compiled but while running it provides a default output instead of characters matrix. Kindly help me in this.

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 {
for(int j=0;j {
ca[i][j]=(char)i;
System.out.print(a[i][j]);
}
System.out.println("");
}
}
}
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,978
Registered: ‎03-06-2006
Message 2 of 11 (228 Views)

Re: Accessing multidimensional char array

I assume this is java.

>ca[i][j]=(char)i;

Any reason you are putting control chars into your array?

You might want to print out "i" and "j" so you know you are going through your nested loops properly.
Please use plain text.
Occasional Advisor
Leepika Mukherjee
Posts: 10
Registered: ‎03-31-2011
Message 3 of 11 (228 Views)

Re: Accessing multidimensional char array

Hi,

I couldnt understand ur question. I want to print matrix of characters(a,b,c,..). I am unable to do it as I m getting default symbols while I run the program. So I need ur help to get proper output, as I am unable to judge where exactly I am missing.
The same program I have made for number matrix and its running exactly as needed. Only for char I couldnt make it out.
Kindly help me in this.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,978
Registered: ‎03-06-2006
Message 4 of 11 (228 Views)

Re: Accessing multidimensional char array

>I'm getting default symbols while I run the program

What default symbols?
You should redirect the output to a file an get a hex dump on that file:
xd -tc -tx1 file

>Only for char I couldn't make it out.

As I said, you are putting control chars into the array, not the characters:
ca[i][j]=(char)i;

Leave this out if you want that initial "a" ... "d".
Please use plain text.
Occasional Advisor
Leepika Mukherjee
Posts: 10
Registered: ‎03-31-2011
Message 5 of 11 (228 Views)

Re: Accessing multidimensional char array

Kindly provide me the simplest program which can display multidimensional car array. I am getting confused so leave the previous program and guide me in a simplest way.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,978
Registered: ‎03-06-2006
Message 6 of 11 (228 Views)

Re: Accessing multidimensional char array

>provide me the simplest program which can display multidimensional char array.

Only if you want a C source.
Please use plain text.
Occasional Advisor
Leepika Mukherjee
Posts: 10
Registered: ‎03-31-2011
Message 7 of 11 (228 Views)

Re: Accessing multidimensional char array

I mean to say the program should be in Java.. Kindly help me.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,978
Registered: ‎03-06-2006
Message 8 of 11 (228 Views)

Re: Accessing multidimensional char array

>I mean to say the program should be in Java

I can't help you there.
But as I said, try removing: ca[i][j]=(char)i;
Please use plain text.
Occasional Advisor
Leepika Mukherjee
Posts: 10
Registered: ‎03-31-2011
Message 9 of 11 (228 Views)

Re: Accessing multidimensional char array

I already tried removing that but getting error again.totally confused. kindly provide me a similar program in java so that I can understand the error I am doing. For number matrix I had done similar coding as below and its giving exact output :-

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

}
}
Output :-

00000
11111
22222
33333
44444

Kindly help me in coding similar program for displaying char matrix..
a b
c d
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,978
Registered: ‎03-06-2006
Message 10 of 11 (228 Views)

Re: Accessing multidimensional char array

>help me in coding similar program for displaying char matrix.

If you already have these initializers: ca[0][0]='a';
You don't need: ca[i][j]=(char)i;

In C you could do:
ca[i][j]=(char)(i*2 + j + 0x40);
Or even better, assuming ASCII:
ca[i][j]=(char)(i*2 + j + ('a' - 1));
Please use plain text.
Occasional Advisor
Leepika Mukherjee
Posts: 10
Registered: ‎03-31-2011
Message 11 of 11 (228 Views)

Re: Accessing multidimensional char array

Can anyone plzzzz. write provide me the complete java program for this as I have mentioned the coding which I have done and also I have tried removing the ca[i][j]=(char)i; as per ur suggestion but still getting errors.

Kindly provide me the complete program for this.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation