1

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.

7
  • 1
    How would a 2D character array map into one string? Commented 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. Commented 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. Commented 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 array Commented 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? Commented Oct 15, 2010 at 3:39

4 Answers 4

1
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!!!

Sign up to request clarification or add additional context in comments.

3 Comments

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

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

0

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

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

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

4 Comments

How would I start something like that. I'm a beginner Java programmer.
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.
The assignment calls for reading the input file and placing the data into a 10x10 array of characters.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.