Thread: how to convert a string into a 2d char array?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    how to convert a string into a 2d char array?

    Hey guys, a little help here.I have this string that I need to convert into a 2d char array like this:String str= "A,E,B,I,M,Y#N,R,C,A,T,S";I know how to use delimiter and split string and I know how to convert but only from string to char[].I need something like this:Input: String str= "A,E,B,I,M,Y#N,R,C,A,T,S";Output: [A] [E] [B] [I] [M] [Y][N] [R] [C] [A] [T] [S]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I know how to use delimiter and split string and I know how to convert but only from string to char[]
    OK, so start by splitting at the #
    Then split each one by ,
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    yeah, i thought of that a few seconds after i posted this thread. i'm currently trying that idea out.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Code:
    import java.*;
    public  class Test{
    
    
            public String[] getCipherText(String str3){
            String[] array3= str3.split("#");
            String[] a1= array3[0].split(",");
            return a1;    
        }
    
    
        public static void main(String[] args){
                
            String str3;
            str3= "A,B,E,I,M,Y#N,R,C,A,T,S";
            //cph.setCipherText();
              
            System.out.print("Message: "+cph.getCipherText(str3));
            System.out.print("\n");
        }
    }

    I'm having an error here saying: Message: [Ljava.lang.String;@6a5bc8c9
    What's the solution for this?


    But when I change the method into void and print the string array, it works:


    Code:
    import java.*;
    public  class Test{
    
    
            public void getCipherText(String str3){
            String[] array3= str3.split("#");
            String[] a1= array3[0].split(",");
            for(int i=0;i<a1.length;i++)
                System.out.print(a1[i]);
        }
    
    
        public static void main(String[] args){
                
            String str3;
            str3= "A,B,E,I,M,Y#N,R,C,A,T,S";
            //cph.setCipherText();
              
            cph.getCipherText(str3);
            System.out.print("\n");
        }
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by UltimoBrah View Post
    yeah, i thought of that a few seconds after i posted this thread. i'm currently trying that idea out.
    Perhaps you could also think about why posting Java code on a C board might not be the best idea in the world.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    oh yes, the .......... am i doing. sorry, just got ..........ed up.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When I needed some assistance for Java, I used this forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  2. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  3. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  4. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM
  5. convert string to char array
    By Dan17 in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2006, 11:47 PM

Tags for this Thread