tags:

views:

70

answers:

4

I'm trying to use a string constructor to convert the 2d char array into a string. Having problems finding the proper constructor. I attempted using various constructors but nothing seems to work.

A: 

What do you mean a 2d char array into a string?

If you have this: {{'a', 'b', 'c'}, {'d', 'e', 'f'}}

Do you want the result to be: "abcdef"?

RyanHennig
I'm reading in an input file of characters. I'm placing the characters in a char array. Now I want to display the array into a GUI. But the setText method only takes in strings.
Mister Bunker
A: 

You want to turn a 2d char array into a single string? Seems kind of strange. Do you want to just concatenate each row at the end of the last row?

Long story short, I don't think you will find a built in constructor to do that. You are probably going to have to write something to do that conversion on your own.

EDIT:
Based on your comments it looks like you are using the wrong data structure when you originally read the file. Here is a code snippet to read an entire file into a string so you don't have to do any conversion later.

  StreamReader MyStreamReader = new StreamReader(@"c:\Projects\Testing.txt");
  string fileContents= MyStreamReader.ReadToEnd();
  MyStreamReader.Close();
JohnFx
How would I start something like that. I'm a beginner Java programmer.
Mister Bunker
Based on your comment on Ryan's answer. I think you need to take one step back to the core problem. You should read the data in one line at a time into a string or 1d character array and the problem is moot. If you post your code that reads the file, I can help you in more detail.
JohnFx
The assignment calls for reading the input file and placing the data into a 10x10 array of characters.
Mister Bunker
Ahhhh. Homework, a mythical land of hypothetical and impractical problems. =) In that case you probably just need to write a nested loop to concatenate each element of the 2d array into a string.
JohnFx
A: 
char chars[][]= {{'a', 'b', 'c'}, {'d', 'e', 'f'}};

        StringBuilder sb = new StringBuilder();

     for(int i = 0; i<2 ;i++){

         for(int j =0; j<3; j++){


             sb.append(chars[i][j]);

         }

     }


        System.out.print(sb.toString());

this is one of my option to do .. nevertheless.. there might be a good code!! look for it!!!

Vanji
@Vanji I tried your method but its saying that setText can't be used with string builder.
Mister Bunker
@Mister Bunker - Hint - look at the javadocs for StringBuilder.
Stephen C
@Mister Bunker There is no method call setText in StringBuilder and and i did not use it !!
Vanji
+1  A: 

This will give you a list of String's

    char[][] arr={{'a', 'b', 'c'}, {'d', 'e', 'f'}};
    List<String> list=new ArrayList<String>();
    for(char[] ar:arr)
    {
        list.add(new String(ar));
    }

If you want the 2d char array as a single string:

    StringBuilder b=new StringBuilder();//use string builder instead of list
    for(char[] ar:arr)
    {
        b.append(new String(ar));
    }

If you simply want to print the char 2d array:

Arrays.deepToString(arr));
Emil