Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 25th, 2012, 03:20 PM
jasondj jasondj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 93 jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 5 h 54 m 51 sec
Reputation Power: 24
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]);
				}
			}
		}
	}

Reply With Quote
  #2  
Old October 30th, 2012, 03:35 AM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
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

Reply With Quote
  #3  
Old October 30th, 2012, 04:01 AM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Can you help me optimize this code?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap