Results 1 to 19 of 19
  1. #1
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default 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.

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    please state the error you are getting, that way we can help you better
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    an one more thing, notice the difference between these two, when they are supposedly have to perform the same task

    int[][] twod=new int[5][5];
    char ca=new char[2][2];
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

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

  5. #5
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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.
    When in doubt, use a lightsaber

  6. #6
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  7. #7
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    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
    Last edited by baf06; 04-12-2011 at 05:13 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    Quote Originally Posted by leepikamukharji View Post
    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
    Java Code:
    ca[i][j]=(char)i;
    to do?

    And (when posting code) can you please use code tags.

  9. #9
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    I have no idea what you want.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

    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)

  12. #12
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    I just want a simplest program which displays multidimensional char array or matrix of char array...

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    Which has nothing to do with your problem about char[i][j] = (char)i;

    What are you trying to do there?

  14. #14
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    I am confused, kindly provide me a simple program for that.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    Your code bove:
    Java 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.

  16. #16
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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

  17. #17
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Your last code does not store the values in the array.

    Java 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();
    		}
    	}
    Swastik

  18. #18
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    Thanks a lot :)

    I m really really thankful to you .... Atlast I got answer to my question.

  19. #19
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

Similar Threads

  1. Multidimensional Array help
    By Gaebril in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 09:36 PM
  2. Replies: 2
    Last Post: 03-08-2011, 07:20 AM
  3. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 08:24 AM
  4. Navigate through a multidimensional array
    By VinTiger in forum New To Java
    Replies: 4
    Last Post: 05-05-2009, 08:16 PM
  5. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-23-2009, 12:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •