0

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?

Nick Meyer
  • 1,771
  • 1
  • 17
  • 29
  • 4
    That's because they're numbers, they're not `String`s anymore. Just keep them as `String`s. – Luiggi Mendoza Aug 12 '14 at 21:08
  • then save them as string and when you need it as number convert it? – Marco Acierno Aug 12 '14 at 21:08
  • I read your edit and still don't get why you cannot keep them as `String`s. Please provide your exact problem and we will try to help you. Also, if you have a fixed length for your files, then use the possible duplicate Q/A. – Luiggi Mendoza Aug 12 '14 at 21:23
  • Based on your edit, you'll need to convert it to a number when you need it, as @MarcoAcierno said. – Andrew Aug 12 '14 at 21:24
  • An `int` is always 32 bits. When you convert it to character form to display it, you can choose to have leading zeros or not. That's not an attribute of the `int` but rather just a function of how you convert it to character form. – Hot Licks Aug 12 '14 at 21:25
  • Are the methods `findLargest` and `arrayMin` custom ones that you have written? If so, can you post the code for those also? Your problem is actually pretty simple to solve, you just have to modify those methods to accept `String` arrays and return `String`s – Nick Meyer Aug 12 '14 at 21:28
  • 1
    What do you want to do if you get files `00123.jpg` and `102112.jpg` (different length). Also, what makes you think `(max + min)/2` will match an existing file? It would help a lot if you could explain what you're trying to accomplish, as I expect this is an XY problem. – Jim Garrison Aug 12 '14 at 22:01
  • 1. It's the same length – Machlev Development Aug 13 '14 at 07:03
  • 2. I take the file in the middle between the file with the largest name and the smallest name (In numbers) – Machlev Development Aug 13 '14 at 07:04

3 Answers3

0

Store it in a HashMap<String, integer>

HashMap<String, int> map = new HashMap<String, int>();
for(int I = 0; I < numbers.length; I++)
    map.put(fileNames[I], Integer.parseInt(fileNames[I].replace(".jpg", "")));

Something to that effect.

Cheruvian
  • 5,628
  • 1
  • 24
  • 34
  • 3
    What's the point, since getting the integer from the String key is trivially done by `Integer.parseInt(key)`? – JB Nizet Aug 12 '14 at 21:25
  • *shrug depending on what operations and how many OP needs to do, trivial may begin to add up. tis an issue of Memory vs CPU. That beign said, for a simple average, adding them all on demand is probably a better solution. I'm just trying to answer the question. – Cheruvian Aug 12 '14 at 21:27
0

Something like this might help:

for (int i = 0; i < fileNames.length; i++)
{
    fileNames[i] = fileNames[i].replace(".jpg", ""); //Removes .jpg from the file names
}
ZeroNum[] numbers = new ZeroNum[];
for (int i = 0; i < numbers.length; i++)
{
    numbers[i].zeros = fileNames[i].length() - Integer.toString(Integer.parseInt(fileNames[i])).length(); // total length of string minus non-zero
    numbers[i].num = Integer.parseInt(fileNames[i]);
}

class ZeroNum
{
    int num, zeros;
    public ZeroNum(int num, int zeros)
    {
        this.num = num;
        this.zeros = zeros;
    }
}

I get that this is a pretty ugly solution, but it should work...

KosherBacon
  • 172
  • 1
  • 11
  • This may be one way of going about the problem, but OP would be better off redesigning the program to use `String` arrays and converting to int when needed for calculations. – Nick Meyer Aug 12 '14 at 21:39
  • 1
    @Aristocrates you're absolutely right. I was just providing a possible solution to the problem. Without further context, and knowing what the rest of the code looks like, it's hard to say what's easier to use. I would personally just use string arrays like you mention. – KosherBacon Aug 12 '14 at 23:33
  • numbers[i].zeros is always 0. – Machlev Development Aug 13 '14 at 07:51
  • @MachlevDevelopment it shouldn't be because `Integer.parseInt()` will strip the existing zeros. So subtracting the character length of that from the original string results in the number of zeros removed. – KosherBacon Aug 13 '14 at 14:46
  • @GLaDOS I get Cannot invoke toString() on the primitive type int Error – Machlev Development Aug 13 '14 at 18:11
  • @MachlevDevelopment look at the change I made. – KosherBacon Aug 13 '14 at 20:48
0

By converting to an int, you've asked java to represent the String as a int value. Note, that the 'int' value of 00123 is actually 123. When you do the conversion, the extra zero's are discarded due to being, as you said, 'useless'.

If you wanted to keep them I'd recommend simply keeping them as strings and not converting them. However, perhaps you have a need so one relatively easy solution would be constructing/using a secondary array that contains the original length of each String-to-be-converted. Then, after doing your logic, use that secondary array to convert the numbers back to strings with the appropriate number of leading zeros. In this case, your secondary array could actually be the 'filenames' array.

    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
}

/*do your logic that required the int conversion, keeping the numbers array in the same order */

/* note, this will only keep the ORIGINAL NUMBER OF CHARACTERS in the filename by padding zeros at the front... 
 * it will NOT maintain the original number of zeros. */
String [] newFileNames = new String[numbers.length];
for (int i = 0; i<numbers.length; i++) {
    numbers[i] = Integer.parseInt(fileNames[i]); //Convert the file names to int
}

for (int i = 0; i<numbers.length; i++) { // for each number in numbers
    int numberDigits = String.valueOf(numbers[i]).length(); //  number of digits in numbers[i]
    int filenameDigits = fileNames[i].length(); // number of digits originally in the filename
    newFileNames[i] = "" + numbers[i];
    for(int zerosToAdd = filenameDigits - numberDigits; zerosToAdd>0; zerosToAdd--) {
        newFileNames[i] = "0" + newFileNames[i];  // add a zero to the front
    }
}

Obviously you could do this with an actual object that contained both the String and the filename -- and I'd say that'd likely be preferable, but this is a fast and dirty solution.

Now, note that this only adds zeros up to the original number of characters... so if fileNames[2] was 0099 got converted to numbers[2] and was just '99'... and then your logic modified that to 123... the above code would only add one zero instead of two... returning it to the 4 digits of the original filename. Clearly one could use the same logic and simply count the initial zeros in the filename (numberOfInitialZeros = fileNames[i].length-fileNames[i].replace("0","").length()) and add them back in, but I thought this was likely the more wanted solution.

lowcrawler
  • 6,777
  • 9
  • 37
  • 79