简体   繁体   中英

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. 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). 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. 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 . Try this simpler code, to create 10 file names. 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 . Without parenthesis, i and 1 are cast to string separately, and are then concated to the string. For example, if i is 5 , then without parenthesis this gives 51 , whereas with parenthesis you get 6 .

Bonus : You can pad the number part with 0 s like this:

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.

For example: 45 becomes 0045 , 12345 stays 12345

The value that you are trying to assign to emp[count] is the ascii value of 1

What you want is the ascii value of 1 which is 49, so you could do

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.

See the answer from @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 . So you can do all of this in a single line:

String file = base + 1 + extension;

Notice that 1 is not special here. It is a value with type 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.

emp[count] = (char)1;

Try this instead:

emp[count] = '1';

Edit: If you want it more dynamically

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

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