【问题标题】:Java convert individual characters from char array to String arrayJava将字符数组中的单个字符转换为字符串数组
【发布时间】:2018-02-26 19:15:51
【问题描述】:

我正在从文本文件中读取一个单词(程序),并且我想将它存储到我的二维数组中,名为word1。为此,我读入文件并存储到占位符数组中。然后,我将此占位符数组转换为 char 数组,以便拆分每个字母。我现在想将此字符数组中的单个字母发送回我之前创建的字符串数组 (word1)。最终,我希望word1 数组变成这样

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

一切正常,直到我尝试将 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
   }

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    String 中没有定义 String(char) 构造函数。所以你不能这样做:

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

    你需要的是String.valueOf(char c)

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

    【讨论】:

      【解决方案2】:

      要在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);
      }
      

      测试

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

      输出

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

      哦,抱歉,应该是 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();
      }
      

      测试

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

      输出

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

      【讨论】:

        【解决方案3】:

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

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

        更好——封装了新的String调用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-17
          • 1970-01-01
          • 2017-11-27
          • 2023-03-19
          • 2012-04-17
          相关资源
          最近更新 更多