简体   繁体   中英

Java convert individual characters from char array to String array

I'm reading from a text file a word (program) and I want to store it into my 2d array called word1 . To do this, I read in the file and store into a placeholder array. I then convert this placeholder array into a char array so that every letter is split up. I now want to send the individual letter from this char array back to the string array ( word1 ) that I created earlier. Ultimately, I want the word1 array to become like this

String word1[][] = {
   {"p", "*"}, {"r", "*"}, {"o", "*"}, {"g", "*"}, {"r", "*"}, {"a", "*"}, {"m", "*"},
};

Everything works up until the very last bit where I try to convert the individual letters from the char array back into the word1 array.

FileReader file = new FileReader("C:/Users/Mark/Desktop/Java/Workshop 2/hangman.txt");
BufferedReader reader = new BufferedReader(file);

String text = "";
String line = reader.readLine(); //Keeps reading line after line 
while (line != null){
  text += line;
  line = reader.readLine();
}

String word1[][] = {
  {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"}, {"", "*"},
};

String placeholder[] = text.split("\\s+");   //Converts text into an array

String s = "";
   for (String n:placeholder){
    s+= n;
  }

char[] charArray = s.toCharArray();

   for (int i = 0; i < 6; i++){
     word1[i][0] = new String(charArray[i]); //This is where problem occurs
   }

No String(char) constructor is defined in String . So you cannot do :

String word1[][]  = ...;
word1[i][0] = new String(charArray[i]); //This is where problem occurs

What you need is String.valueOf(char c) :

word1[i][0] = String.valueOf(charArray[i]); 

To convert back and forth between String and char[][] , use these methods:

public static char[][] toCharArray(String text) {
    char[][] c = new char[text.length()][2];
    for (int i = 0; i < c.length; i++) {
        c[i][0] = text.charAt(i);
        c[i][1] = '*';
    }
    return c;
}

public static String toString(char[][] c) {
    char[] buf = new char[c.length];
    for (int i = 0; i < c.length; i++)
        buf[i] = c[i][0];
    return new String(buf);
}

Test

char[][] word1 = toCharArray("program");
System.out.println(Arrays.deepToString(word1));

String text = toString(word1);
System.out.println(text);

Output

[[p, *], [r, *], [o, *], [g, *], [r, *], [a, *], [m, *]]
program

Oh sorry, that was supposed to be String to/from String[][] :

public static String[][] toArray2D(String text) {
    String[][] arr = new String[text.length()][];
    for (int i = 0; i < arr.length; i++)
        arr[i] = new String[] { text.substring(i, i + 1), "*" };
    return arr;
}

public static String toString(String[][] arr) {
    StringBuilder buf = new StringBuilder();
    for (String[] a : arr)
        buf.append(a[0]);
    return buf.toString();
}

Test

String[][] word1 = toArray2D("program");
System.out.println(Arrays.deepToString(word1));

String text = toString(word1);
System.out.println(text);

Output

[[p, *], [r, *], [o, *], [g, *], [r, *], [a, *], [m, *]]
program

String text = String.copyValueOf(data);

or

String text = String.valueOf(data);

is better - encapsulates the new String call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM