Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes trouble making efficient  java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "trouble making efficient  java program" Watch "trouble making efficient  java program" New topic
Author

trouble making efficient java program

Ravi Kapoor
Greenhorn

Joined: Apr 27, 2008
Posts: 2
Can somebody help me make an efficient java program for the below problem:

Question : A client application needs to determine if the server to which it is connecting is compatible. The server will inform the client which versions it supports, for instance responding with a min_version="5.0.1.1234" and a max_version="5.4.12.9999". The client application needs to compare these versions with its own build
version, for instance build_version="5.2.6.2345" would be compatible. Please produce client code using Java that will indicate to the user whether the client is:
a) compatible with the server
b) older than the minimum supported version
c) newer than the maximum supported version
You can assume that you already have the relevant strings containing the client build version and the supported server versions so there is no need to write code to communicate with the server.
The first numbers in the version string are most significant, so "5.0.1.1234" will be a more recent version than "4.99.999.9999"


currently my code is not functioning properly.It does not work properly for a client_version = "5.4.12.1000".I have tokenized the string values of the versions and converted into arrays. Then I try to compare the array values as the code below :



- Pls help me where I am wrong.
- Is there any other efficient way of doing this which saves some lines of code or improves performance?

[ April 27, 2008: Message edited by: Ravi Kapoor ]
[ April 27, 2008: Message edited by: Jim Yingst ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Welcome to the Ranch, Ravi.

Please use an informative title for your questions.
No, we don't hand out solutions ready-made, but please tell us what you have got so far and we shall say what we think of it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Please note the CODE button below the window; it makes the code much easier to read.

You are confusing loops and selection; you probably can lose all the loops and "breaks" which are often considered poor practice (see here no 3.1). You presumably only want to go through the whole procedure once, anyway.

You can probably get the whole thing to work with a set of if-elses. Or even boolean assignment.

boolean tooOld = clientarr[0] < minarray[0] || clientarray[0] == minarray[0] && clientarray[1] < minarray[1] || etc.

I presume you have used the String.split() method to separate the Strings into arrays, and used the Integer.parseInt() method to convert it into ints? I presume you have used an appropriate regular expression to split?
If you have left the tokens in the form of a String[] array, you can use the Comparable<String> interface which String implements:

. . . clientarr[0].compareTo(minarray[0]) < 0 . . .

That will work for numbers as long as you have strings containing only digits 0123456789.

boolean tooNew = clientarr[0] > maxarray[0] || etc etc.

boolean compatible = !tooOld && !tooNew;
Ravi Kapoor
Greenhorn

Joined: Apr 27, 2008
Posts: 2
Originally posted by Campbell Ritchie:
Please note the CODE button below the window; it makes the code much easier to read.

You are confusing loops and selection; you probably can lose all the loops and "breaks" which are often considered poor practice (see here no 3.1). You presumably only want to go through the whole procedure once, anyway.

You can probably get the whole thing to work with a set of if-elses. Or even boolean assignment.

boolean tooOld = clientarr[0] < minarray[0] || clientarray[0] == minarray[0] && clientarray[1] < minarray[1] || etc.

I presume you have used the String.split() method to separate the Strings into arrays, and used the Integer.parseInt() method to convert it into ints? I presume you have used an appropriate regular expression to split?
If you have left the tokens in the form of a String[] array, you can use the Comparable<String> interface which String implements:

. . . clientarr[0].compareTo(minarray[0]) < 0 . . .

That will work for numbers as long as you have strings containing only digits 0123456789.

boolean tooNew = clientarr[0] > maxarray[0] || etc etc.

boolean compatible = !tooOld && !tooNew;


-------------------------------------------

Hey thanks that really helps make the code cleaner.
However it is still not functionaing as it should. For a value of client_build_version = "5.1.1.1233" the code fails saying its Older version, however this lies between the min & max ranges and hence should be compatible.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Please use code tags to make your code readable. I added them to your post above. However some of the indentation still seems... random. Please do indent your code for readability in the future.

I don't think that using String's compareTo() method is very useful here. The problem is that it sorts by alphabetical order, not numeric order. These are not the same:

1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
3
4
5
6
7
8
9

These numbers are in alphabetic order, but not numeric order. You will be much better off using an array of ints instead of an array of Strings here, I think.


"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Originally posted by Jim Yingst:
I don't think that using String's compareTo() method is very useful here.
Yes, I forgot that point. It would work for versions 0000 to 9999 but not 1 to 9999.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
How are you splitting the String? You're not using split(".") are you? Go through the Java Tutorial about regular expressions, where you find that . doesn't mean what you think it means; it can match anything except "line termination." When you go through the tutorial you find you have to escape it and you end up using "\\."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble making efficient java program