How Do I Check for Extensions?

Greetings!

How would I do a check for extensions?

For example, if I'd like to check the database for an image file name, and if the image file name has extension ".jpg" or ".jpeg" or ".gif", to not append the ".gif" extension.

However, if the ".someExtension" does not exist, I want to append the ".gif" extension.

In pseudocode something like this:

public String getBannerImage(){
  String bImage="";
  String bName=(String)storedValueForKey("bannerImageFile");
  if bName ends with .someExtension
       do Nothing;
  else
    bName= add Extension( (bName)+".gif");
}

Any assistance or guidance would be greatly appreciated!

Thanks!
Jamie
jmc430Asked:
Who is Participating?
 
DrWarezzConnect With a Mentor Commented:
Here's something in Java, that should work:

    public String getBannerImage( ) {
       
        String bName=(String)storedValueForKey( "bannerImageFile" );
       
        StringTokenizer st = new StringTokenizer( bName, "." );
       
        int temp = st.countTokens();
        String sTemp = "";
       
        for ( int i=0; i<temp; i++ )
            sTemp = st.nextToken();
       
        if ( sTemp==null ) return;
       
        String [] ext = { "jpg", "gif", "jpeg" };
        boolean bTemp = false;
       
        for ( int i=0; i<ext.length; i++ ) {
            if ( sTemp == ext[i] ) bTemp = true;
        }
       
        if ( !bTemp ) {
            // Rename file, and add extension:
            File file = new File( bName );
            File file2 = new File( bName + ".gif" );
           
            boolean success = file.renameTo(file2);
            if ( !success ) {
                System.out.println( "File Could Not Be Renamed." );
            }
        }
       
    }


Note: you need to import the:   java.io.*   and   java.util.*  packages, for the above method to work.

gL,
[r.D]
0
 
Mikal613Commented:
you would do an right(filename,Instr(filename,".") ) and that gets the ext
0
 
jmc430Author Commented:
is that in java?
0
 
jmc430Author Commented:
eh .. nevermind
0
 
DrWarezzCommented:

So, how would you like this method/function to be used? How is the filename passed, and if there exists an extension, does the user pass the filename to the method/function?

Also, what language do you want it in? ...... Java?

[r.D]
0
All Courses

From novice to tech pro — start learning today.