简体   繁体   English

Java将单个字符从char数组转换为String数组

[英]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 . 我正在从文本文件中读取一个单词(程序),我想将其存储到名为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. 然后,我将此占位符数组转换为char数组,以便将每个字母拆分。 I now want to send the individual letter from this char array back to the string array ( word1 ) that I created earlier. 现在,我想将这个char数组中的单个字母发送回我先前创建的字符串数组( word1 )。 Ultimately, I want the word1 array to become like this 最终,我希望word1数组变成这样

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. 一切工作直到最后一位尝试将char数组中的各个字母转换回word1数组为止。

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 . 没有在String定义String(char)构造函数。 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) : 您需要的是String.valueOf(char c)

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

To convert back and forth between String and char[][] , use these methods: 要在Stringchar[][]之间来回转换,请使用以下方法:

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[][] : 哦对不起,这应该是String到/从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); 字符串文本= String.copyValueOf(data);

or 要么

String text = String.valueOf(data); 字符串文本= String.valueOf(data);

is better - encapsulates the new String call 更好-封装新的String调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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