将String转换为int但保持长度

时间:2014-08-12 21:07:11

标签: java string

我正在尝试从目录中获取文件名到String,从String中删除文件扩展名,这样我就可以将文件名转换为整数(文件名是数字,如00123.jpg)。

(我想将其转换为int以获取数组中最大数字和最小数字之间的平均数字)

这是我到目前为止所做的:

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
}

这里我再次添加文件扩展名,以便我可以搜索文件:

int max = findLargest(fileNames);
int min = arrayMin(fileNames);
int fileNumber = (max + min)/2;
String fileName = fileNumber + ".jpg";

这是我的问题:numbers[]返回文件名但删除任何无用的零(用123替换00123)。

文件名上的零数在设备之间发生变化(我使用包含这些文件的系统文件夹),我的应用程序在不同的设备上崩溃,因为它查找的是123.jpg而不是00123.jpg(我稍后再添加文件扩展名。)

如何将String转换为int但保留前导零?

3 个答案:

答案 0 :(得分:0)

将其存储在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", "")));

这样的事情。

答案 1 :(得分:0)

这样的事情可能有所帮助:

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

我知道这是一个非常难看的解决方案,但它应该有用......

答案 2 :(得分:0)

通过转换为int,您已经要求java将String表示为int值。请注意,00123的'int'值实际上是123.当你进行转换时,额外的零被丢弃,因为正如你所说的那样,'无用'。

如果您想保留它们,我建议您只是将它们保留为字符串而不是转换它们。但是,也许您需要这样一个相对简单的解决方案是构造/使用包含每个待转换字符串的原始长度的辅助数组。然后,在执行逻辑之后,使用该辅助数组将数字转换回具有适当数量的前导零的字符串。在这种情况下,您的辅助数组实际上可能是'文件名'数组。

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

显然你可以用一个包含字符串和文件名的实际对象来做这件事 - 我会说这可能更好,但这是一个快速而肮脏的解决方案。

现在,请注意,这只会将零添加到原始字符数...所以如果fileNames [2]被0099转换为数字[2]并且只是'99'...然后你的逻辑被修改到123 ...上面的代码只会添加一个零而不是两个...将其返回到原始文件名的4位数。显然,人们可以使用相同的逻辑并简单地计算文件名中的初始零(numberOfInitialZeros = fileNames[i].length-fileNames[i].replace("0","").length())并将它们重新添加,但我认为这可能是更需要的解决方案。

相关问题