Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Converting a 2-D char array into a string

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Converting a 2-D char array into a string

    I have a char array called testArray and currently its holding for example a,b,c,d in its elements. I am trying to convert the letters in the char array into a string and theres a problem. new String(testArray); is underlined and the error is: the constructor string (char[][]) is undefined. I have populated my array using a for loop.

    char testArray [] []= new char [5] [10];
    String newString1 = new String(testArray);
    		System.out.println(newString1);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,101
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Converting a 2-D char array into a string

    Can you explain where the char in the 2 dim array would go in the String?
    If the contents go row by row, you could build Strings using the contents of each row (a row is a one dim array of char) and concatenate the Strings together.

    Another way would be to use a StringBuilder class object and append the char values from the array to it one by one from loops that go over the contents of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Converting a 2-D char array into a string

    Oh I just wanted to make the string to be abcd if the abcd was the result of the char array. I outputted results of a char array and I wanted to remove spaces in the output. Can you use trim for that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,101
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Converting a 2-D char array into a string

    You will have to use a loop to go through the 2 dim array to get the char values into the String.
    If the spaces are at the end of the String, the trim() method will remove them. If the spaces are in the middle of the String, you'll need to use the replace() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  3. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 09:04 AM
  4. Converting Bytes into Char
    By unCrypticSolutions in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 17th, 2012, 03:28 PM
  5. converting int to char
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 11:59 PM