如何比较整数将字符串包含在循环中

时间:2019-02-24 03:41:04

标签: java

我想比较文件名“ AB123456”,“ DF321654”和“ KR852963”的编号。该代码只能显示较小和最大的数目,而不能显示全名,例如“ AB”,“ DF”和“ KR”。请指教我!

  public static void main(String[] args) {

        File folder = new File("input/");
        File [] files = folder.listFiles();      

        //Set first number as smallest and largest   
        long smallest = getNumberFromName(files[0]);
        long largest = smallest;

        //Get number for each file and assign smallest & largest values
        for(int i = 1; i < files.length-1;i++) {
            long nextNumber = getNumberFromName(files[i]);
            if (smallest > nextNumber)
                smallest = nextNumber
            if (largest < nextNumber)
                largest = nextNumber
        }
        System.out.println("The smallest number="+smallest)
        System.out.println("The biggest number="+largest)
    }    

    private static long getNumberFromName(File nextFile) {
        String fileFullName = nextFile.getName();       
        String fileSimple= fileFullName.substring(2,fileFullName.length()-4);           
        return Long.parseLong(fileSimple);
    }

4 个答案:

答案 0 :(得分:1)

您可以使用另外两个字符串来存储原始字符串vlaue,然后将其输出,由于;检查中缺少if,因此代码也无法编译

  public static void main(String[] args) {

        File folder = new File("input/");
        File [] files = folder.listFiles();      

        //Set first number as smallest and largest   
        long smallest = getNumberFromName(files[0]);
        long largest = smallest;
        String smallestStr,largestStr = null;

        //Get number for each file and assign smallest & largest values
        for(int i = 1; i < files.length-1;i++) {
            long nextNumber = getNumberFromName(files[i]);
            if (smallest > nextNumber){
                smallest = nextNumber;//you have missing ; here
                smallestStr = files[i].getName();
            }
            if (largest < nextNumber){
                largest = nextNumber;//you have missing ; here
                largestStr = files[i].getName();
            }
        }
        System.out.println("The smallest number="+smallestStr)
        System.out.println("The biggest number="+largestStr)
    }    

    private static long getNumberFromName(File nextFile) {
        String fileFullName = nextFile.getName();       
        String fileSimple= fileFullName.substring(2,fileFullName.length()-4);           
        return Long.parseLong(fileSimple);
    }

答案 1 :(得分:0)

您应该为完整文件名保留额外的状态。下面对您的代码进行的唯一主要更改是,遇到新的最小或最大数字时,除数字前缀外,我们还将记录完整的文件名。

File folder = new File("input/");
File[] files = folder.listFiles();      

long smallest = getNumberFromName(files[0]);
long largest = smallest;
String sFile = files[0].getName();
String lFile = sFile;

for (int i=1; i < files.length; i++) {
    long nextNumber = getNumberFromName(files[i]);
    if (smallest > nextNumber) {
        smallest = nextNumber;
        sFile = files[i].getName();
    }
    else if (largest < nextNumber) {
        largest = nextNumber;
        lFile = files[i].getName();
    }
}

System.out.println("The smallest number = " + smallest + ", file = " + sFile);
System.out.println("The largest number = " + largest + ", file = " + lFile);

请注意,您当前的方法已经相当精简,因为它不会在迭代时存储整个集合。相反,您只跟踪最小和最大条目。

答案 2 :(得分:0)

您可以尝试以下方法:

private static long getNumberFromName(File nextFile) {
    String fileFullName = nextFile.getName();
    String retStrLong = "";
    for(int i = 0; i < fileFullName.length()-1; i++){

       try{
          Long.parseLong(fileFullName[i]);
          retStrLong = retStrLong+fileFullName[i];
       }catch(){
          continue;
       }
    }
    return Long.parseLong(retStrLong);
}

答案 3 :(得分:0)

我们可以将文件名与正则表达式匹配,如果匹配则获取编号,否则您可以决定要做什么,现在,如果文件名与正则表达式不匹配,我将返回最小的长值。请参考下面的代码。

private static long getNumberFromName(File nextFile)
{
    /*This is th regex for the file name,
    starts with any combination of string values and will have to end with some number*/
    String fileNameRegex = "^([a-zA-Z]*)([0-9]+)$";
    Pattern pattern = Pattern.compile(fileNameRegex);
    Matcher matcher = pattern.matcher(nextFile.getName());
    // If the pattern matches we will return the long value present in the name else the min of the long.
    return matcher.find() ? Long.valueOf(matcher.group(2)) : Long.MIN_VALUE;
}