I am trying to get file names from a directory to a String, remove the file extensions from the String so I can convert the file names to Integers (The file names are numbers, like 00123.jpg).
(I want to convert it to an int to get the average number between the largest number and the smallest number of the array)
Here's what I did so far:
File currentBoot = new File(destination); //Directory
String[] fileNames = currentBoot.list(); //Returns array of file names (Including .jpg)
for (int i = 0; i<fileNames.length;i++)
{
fileNames[i] = fileNames[i].replace(".jpg", ""); //Removes .jpg from the file names
}
int numbers[] = new int[fileNames.length];
for (int i = 0; i<numbers.length; i++) {
numbers[i] = Integer.parseInt(fileNames[i]); //Convert the file names to int
}
Here I add the file extension again so I can search for the file:
int max = findLargest(fileNames);
int min = arrayMin(fileNames);
int fileNumber = (max + min)/2;
String fileName = fileNumber + ".jpg";
Here's my problem: numbers[]
returns the file names but removes any useless zeros (Replaces 00123 with 123).
The number of zeros on the file names changes between devices (I use a system folder with those files in it) and my app crashes at different devices because it looks for 123.jpg
instead of 00123.jpg
(I add the file extensions back later).
How do I convert a String to an int but keeping the leading zeros?