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