#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    93
    Rep Power
    26

    Can you help me optimize this code?


    Hey all, what I am trying to do is retrieve a version number from a database. Ideally, the version number would be a String in the form of "1.0" or "1.1", but other valid entries are "1.0.6" or "1.1.12.3". Additionally, the code needs to gracefully handle strings that don't fit that format at all, such as "noVersionAvailable" or "asjfdjasdg".

    I've written some code that takes care of this, but I'm really not pleased with my solution. It just looks very sloppy and ugly. Can anyone of you java experts suggest a cleaner way of taking care of this problem?

    Code:
    	//determine version number
    	if (connectionValid) {
    		Statement getVersion = conn.createStatement();
    		String query = "SELECT * FROM <tableName> WHERE [key]='version'";
    		ResultSet rs = getVersion.executeQuery(query);
    		if (rs.next()) {
    			String version = rs.getString("value");
    			if (version != null) {
    				Scanner scanner = new Scanner(version);
    				int[] versionNumber = {0, 0};
    				int i = 0;
    				while (scanner.hasNextInt() && i < 2) { 
    					versionNumber[i] = scanner.nextInt();
    				}
    				if ((versionNumber[0] != 0) && (versionNumber[1] != 0)) {
    					ip.setVariable(EXISTING_DB_VERSION_IA_VARIABLE, versionNumber[0] + ":" + versionNumber[1]);
    				}
    			}
    		}
    	}
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    15
    Looks good to me. The int array is a bit redundant. Also as you are checking for zero, you are not going to pick up version 1.0, etc. Initializing to {-1, -1} might be better.

    Here's an alternate approach:

    Code:
            	Scanner scanner = new Scanner(version);
                    if (scanner.hasNext()) {
                        int firstVersionNumber = scanner.nextInt();
                        if (scanner.hasNext()) {
                            int secondVersionNumber = scanner.nextInt();
                            ip.setVariable(EXISTING_DB_VERSION_IA_VARIABLE, firstVersionNumber + ":" + secondVersionNumber);
                        }
                    }
    Hope this helps
    slink
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    15
    Actually, that isn't going to work. Scanner does not work like that.

    You can split a String into an array of Strings with the split method. Split takes a regular expression so it might look a bit odd.

    Code:
        	String[] parts = version.split("\\.");
                 if (parts.length >= 2) {
                     ip.setVariable(EXISTING_DB_VERSION_IA_VARIABLE, parts[0] + ":" + parts[1]);
                 }
    Hope that helps more
    slink

IMN logo majestic logo threadwatch logo seochat tools logo