简体   繁体   English

如何转换数字并将其存储在char数组中,然后将char数组转换为String,以便输出整数?

[英]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?

I have a char array with four characters stored, and I need to add an integer to the char array and then add .txt to the end of if, then render the whole thing as a string so I can use it to create a file object. 我有一个存储有四个字符的char数组,我需要在char数组中添加一个整数,然后在if的末尾添加.txt,然后将整个内容呈现为字符串,以便可以使用它来创建文件对象。 But when I run the whole process it doesn't work. 但是,当我运行整个过程时,它不起作用。 Using println to output what is going on at every step it shows me that the number stored in the char array is printing to string as this: ( 0001 ) instead of just this (1). 使用println输出每一步发生的事情,它向我展示了存储在char数组中的数字正按以下方式打印为字符串:(0001)而不是仅此(1)。 Why is that and how do I work around it? 为什么会这样,我该如何解决? I typed up a short version of the segment of code here to demonstrate the problem. 我在这里键入了一段代码的简短版本来演示该问题。 The output of the printline statement below is this: temp 0001 .txt instead of temp1.txt which is what I'm trying to get. 下面的printline语句的输出是:temp 0001 .txt而不是temp1.txt,这是我想要的。 Thanks for any help you can offer. 谢谢你的尽心帮助。

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

  }
}

You don't need to take all the hassles with character arrays. 您无需费心使用字符数组。 Java has done most of the job for you with String . Java通过String为您完成了大部分工作。 Try this simpler code, to create 10 file names. 试试这个更简单的代码,创建10个文件名。 I think you are trying to create a number of files with numbered names having same base name. 我认为您正在尝试创建多个具有相同基本名称的编号名称的文件。

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

Note the parenthesis around i+1 . 注意i+1周围的括号。 Without parenthesis, i and 1 are cast to string separately, and are then concated to the string. 不带括号, i1分别转换为字符串,然后被压缩为字符串。 For example, if i is 5 , then without parenthesis this gives 51 , whereas with parenthesis you get 6 . 例如,如果i5 ,则不带括号的结果为51 ,而带括号的结果为6

Bonus : You can pad the number part with 0 s like this: 奖励 :您可以像这样用0填充数字部分:

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

With this, the numbered part is padded with zeros to make length 4 if the length is less than 4. However if the length if 4 or greater than 4, the number does not get stripped. 这样,如果长度小于4,则编号的部分将填充零以使长度为4。但是,如果长度小于或等于4,则不会剥离数字。

For example: 45 becomes 0045 , 12345 stays 12345 例如: 45变为004512345入住12345

The value that you are trying to assign to emp[count] is the ascii value of 1 您尝试分配给emp[count]值为ascii值1

What you want is the ascii value of 1 which is 49, so you could do 你想要的是一个ASCII值1是49,所以你可以做

emp[count] = 49;

or 要么

emp[count] = 48 + 1;

or 要么

emp[count] = '1';

Edit As per your comments, if all that you are trying to do is created a new file name then these arrays are not even needed. 编辑根据您的注释,如果您要执行的所有操作均创建了新文件名,则甚至不需要这些arrays

See the answer from @Code-Apprentice 请参阅@ Code-Apprentice的答案

String file = base + 1 + extension;

Remember that the + operator works specially with strings to concatentate them. 请记住, +运算符专门与字符串配合使用。 On top of that, anything is automatically converted into a String when the other operand of + is a String . 最重要的是,当+的另一个操作数是String时,任何内容都会自动转换为String So you can do all of this in a single line: 因此,您可以在一行中完成所有这些操作:

String file = base + 1 + extension;

Notice that 1 is not special here. 请注意,此处1不是特殊的。 It is a value with type int . 这是一个类型为int的值。 So you can easily replace it with a variable name instead. 因此,您可以轻松地将其替换为变量名。

This will insert the char with the value 1 instead of the character 1. 这将插入值为1而不是字符1的char。

emp[count] = (char)1;

Try this instead: 尝试以下方法:

emp[count] = '1';

Edit: If you want it more dynamically 编辑:如果您想要更动态

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

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

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