【问题标题】:How do you convert a number and store it in a char array and then convert the char array to String so that it prints the integer?如何转换数字并将其存储在 char 数组中,然后将 char 数组转换为 String 以便打印整数?
【发布时间】:2016-05-28 13:44:17
【问题描述】:

我有一个存储了四个字符的 char 数组,我需要在 char 数组中添加一个整数,然后将 .txt 添加到 if 的末尾,然后将整个内容呈现为字符串,以便我可以使用它来创建一个文件对象。但是当我运行整个过程时它不起作用。使用 println 输出每一步发生的事情,它向我显示存储在 char 数组中的数字正在打印为字符串,如下所示:( 0001 ) 而不仅仅是这个 (1)。为什么会这样,我该如何解决?我在这里输入了一段简短的代码来演示这个问题。下面的 printline 语句的输出是这样的: temp 0001 .txt 而不是 temp1.txt 这是我想要得到的。感谢您提供的任何帮助。

public class Test {
  public static void main(String[] args) {
  int count = 4;
  char[] temp = new char[count + 5];
  char[] base = new char[] {'t', 'e', 'm', 'p'};

  char[] extension = new char[] {'.', 't', 'x', 't'};
  for (int i = 0; i < 4; i++)
     temp[i] = base[i];

  temp[count] = (char)1;
  for (int k = 0; k < 4; k++)
     temp[count + 1 + k] = extension[k];

  String file = new String(temp);

  System.out.println(file);

  }
}

【问题讨论】:

  • 有什么理由必须将其保留为数组吗?我的意思是你可以这样做s = new String(base) + Integer.toString(theNumberToAdd) + new String(extension);
  • @FabianN。对Integer.toString() 的显式调用在这里甚至是可选的。
  • @Code-Apprentice 我有点太小心了,因为我做了很多 Javascript atm 并且不想在 Java 中运行 number = '6' * 1
  • 在我的回答中看看更简单的方法来解决您的问题。和我一起唱:当我们可以使用 String 时不要使用 char 数组 :)
  • 我不喜欢 OP 不接受任何回答就走开。我们的努力是徒劳的吗? :(

标签: java arrays char render-to-string


【解决方案1】:

这将插入值为1 的字符而不是字符1。

emp[count] = (char)1;

试试这个:

emp[count] = '1';

编辑: 如果你想要更动态的

int i = ...
emp[count] = (char) ('0'+ i);

【讨论】:

  • 在我正在制作的实际程序中,我必须通过一个循环创建多个具有不同数字的文件名(temp1.txt、temp2.​​txt...等),我怎样才能让它工作这种情况,因为我需要基于将 int 变量从 for 循环转换为数字来分配文件名字符串中的数字?
  • 所以它将是 temp[count] = (char)i;其中 i 是 for 循环中的计数器。
  • 如果i &gt;= 10,这将失败。使用Integer.toString()更安全。
  • 太好了,这正是我所需要的。非常感谢!
  • @JimboSakahana i 需要的最大值是多少?
【解决方案2】:

您尝试分配给 emp[count] 的值是 ascii 值 1

你想要的是 1 的 ascii 值,即 49,所以你可以这样做

emp[count] = 49;

emp[count] = 48 + 1;

emp[count] = '1';

编辑根据您的 cmets,如果您要做的只是创建一个新文件名,那么这些 arrays 甚至都不需要。

查看@Code-Apprentice 的答案

String file = base + 1 + extension;

【讨论】:

    【解决方案3】:

    请记住,+ 运算符专门用于连接字符串。最重要的是,当+ 的另一个操作数是String 时,任何东西都会自动转换为String。因此,您可以在一行中完成所有这些操作:

    String file = base + 1 + extension;
    

    注意 1 在这里并不特殊。它是一个类型为int 的值。因此,您可以轻松地将其替换为变量名。

    【讨论】:

      【解决方案4】:

      您无需为字符数组而烦恼。 Java 使用String 为您完成了大部分工作。试试这个更简单的代码,创建 10 个文件名。我认为您正在尝试创建许多具有相同基本名称的编号名称的文件。

      public class Test {
        public static void main(String[] args) {
          String baseName = "temp";
          String extension = ".txt";
          
          // create 10 file names
          for(int i=0; i<10; i++) {
            String newName = baseName + (i+1) + extension;
            System.out.println(newName);
          }
        }
      }
      

      注意i+1 周围的括号。如果没有括号,i1 将分别转换为字符串,然后连接到字符串。例如,如果i5,那么不带括号则为51,而带括号则为6

      奖励: 您可以像这样使用0s 填充数字部分:

      String newName = baseName + String.format("%04d", i+1) + extension;
      

      这样,如果长度小于 4,编号部分将用零填充以使长度为 4。但是,如果长度为 4 或大于 4,则不会删除数字。

      例如:45 变为 004512345 仍为 12345

      【讨论】:

        猜你喜欢
        • 2016-04-28
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 2011-08-13
        • 2011-08-28
        相关资源
        最近更新 更多