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.
-
1How would a 2D character array map into one string?Cameron– Cameron2010-10-15 03:24:51 +00:00Commented Oct 15, 2010 at 3:24
-
It sounds like you're operating at far lower levels of abstraction than you need to / should be. Why have an array of array of chars? This is Java, not C. If you're reading all the lines of a file, I would suspect you can get an array or List of Strings. In which case you can create a StringBuilder, loop over all the strings, add them to the string builder, and put a "\n" in between each string. Use the resulting string in the setText method.I82Much– I82Much2010-10-15 03:34:25 +00:00Commented Oct 15, 2010 at 3:34
-
@Cameron I was told I had to convert a 2d char array to display the array in a GUI. The setText only takes in a string.Mister Bunker– Mister Bunker2010-10-15 03:35:24 +00:00Commented Oct 15, 2010 at 3:35
-
@I82Much I am building a word mole game for class assignment. You take in a word from JOptionPane, and then compare it to the 10x10 array of characters. If the word matches the letters in the array we erase the letters from the arrayMister Bunker– Mister Bunker2010-10-15 03:38:41 +00:00Commented Oct 15, 2010 at 3:38
-
Why is it not possible to iterate through the 2D-array and buffer each iterated item into a string in the format you want?eee– eee2010-10-15 03:39:00 +00:00Commented Oct 15, 2010 at 3:39
4 Answers
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!!!
3 Comments
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));
Comments
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"?
1 Comment
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();