Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串转换为int但保留长度_Java_String - Fatal编程技术网

Java 将字符串转换为int但保留长度

Java 将字符串转换为int但保留长度,java,string,Java,String,我试图将目录中的文件名转换为字符串,从字符串中删除文件扩展名,以便将文件名转换为整数(文件名是数字,如00123.jpg) (我想将其转换为整数,以获得数组中最大数和最小数之间的平均数) 以下是我到目前为止所做的: File currentBoot = new File(destination); //Directory String[] fileNames = currentBoot.list(); //Returns array of file names (Including .jpg)

我试图将目录中的文件名转换为字符串,从字符串中删除文件扩展名,以便将文件名转换为整数(文件名是数字,如00123.jpg)

(我想将其转换为整数,以获得数组中最大数和最小数之间的平均数)

以下是我到目前为止所做的:

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
}
我的问题是:
numbers[]
返回文件名,但删除任何无用的零(将00123替换为123)

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


如何将字符串转换为int,但保留前导零?

将其存储在
HashMap

HashMap map=newhashmap();
for(int I=0;I

类似这样的东西可能会有帮助:

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;
    }
}
for(int i=0;i

我知道这是一个非常难看的解决方案,但它应该可以工作…

通过转换为int,您已经要求java将字符串表示为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
    }
}
File currentBoot=新文件(目标)//目录
}
字符串[]文件名=currentBoot.list()//返回文件名数组(包括.jpg)

对于(int i=0;这是因为它们是数字,它们不再是
String
s。只需将它们保留为
String
s。然后将它们保存为String,并在需要时将其转换为数字?我阅读了您的编辑,仍然不明白为什么不能将它们保留为
String
s。请提供确切的问题,我们将尽力帮助您。此外,如果你的文件有一个固定长度,那么使用可能的重复Q/a。根据你的编辑,你需要在需要时将其转换为一个数字,正如@Marcocierno所说。如果你得到文件
00123.jpg
102112.jpg
(不同的长度)。还有,你怎么想
(最大+最小值)/2
将匹配现有文件?如果您能解释您试图实现的目标,这将非常有帮助,因为我认为这是一个XY问题。这有什么意义,因为从字符串键获取整数是由
integer.parseInt(key)完成的
?*耸耸肩取决于什么操作和需要执行多少个OP,琐碎的可能开始累积。这是内存与CPU的问题。beign说,对于简单的平均值,按需添加它们可能是更好的解决方案。我只是想回答这个问题。这可能是解决问题的一种方法,但OP最好重新设计签署程序以使用
String
数组,并在需要时转换为int进行计算。@贵族们,你完全正确。我只是提供了一个可能的问题解决方案。如果没有进一步的上下文,并且不知道其余的代码是什么样子,很难说什么更容易使用。我个人只会使用String像您提到的.numbers[i].zero数组始终为0。@这不应该是因为
Integer.parseInt()
将删除现有的零。因此,从原始字符串中减去该字符串的字符长度将导致删除的零数。@GLaDOS i get无法对基元类型int错误调用toString()
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;
    }
}
    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
    }
}