954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

integer comparison

Hi I am creating a house search programme. the objective of this programme is to sell houses within a range specified by the potential buyer. Now the buyer would type in the amount he is willing to spend and the houses closest to this range should be presented to him/her even if they are a long way off, so long as they are the closest.

Many thanks in advance

Grub
Junior Poster in Training
60 posts since Oct 2007
Reputation Points: 14
Solved Threads: 0
 

And what is your question?

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

"do my homework for me", probably.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Sorry, my question is, how do I compare the integer values (price) of the houses to a user defined integer and determine which has the closest value to the user defined integer.

Grub
Junior Poster in Training
60 posts since Oct 2007
Reputation Points: 14
Solved Threads: 0
 

With comparison operators and subtraction.

This would normally be done through a query to a database though, not in your Java code.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

I am afraid the requirement is only through the java code. the logic eludes me.

Grub
Junior Poster in Training
60 posts since Oct 2007
Reputation Points: 14
Solved Threads: 0
 

Well at least give it a try. Post the code and all error/compile messages as well as what it does (not) do that you expected, once you have written something.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
if (a - b > 0) {
 // blah blah blah
}


Sheesh, how hard can it get?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

you migh wanna do a binary search and keep some parameters until you find the closest ones
house1: $10k
house2: $25k
house3: &50k and so on and then search through the array or binary tree depending which one you wanna use to storage your data until you get the closest price
if you wanna compare the value i think you cant do (a - b > 0) you have to do eiher:

int dif = a - b;
if (dif > 0);

or

if ((a - b) > 0)

uonsin
Newbie Poster
17 posts since Oct 2007
Reputation Points: 8
Solved Threads: 2
 

if you wanna compare the value i think you cant do (a - b > 0) you have to do eiher:

int dif = a - b; if (dif > 0);

or

if ((a - b) > 0)

Wrong.

I'm sorry, but, take two minutes to perform a test and you can avoid making false assumptions like this.

Edit: I mean, both of your examples, of course, work, but so does the original, making your "workarounds" unnecessary (although I will often use the second myself, to make maintenance of the code, for some of the novice programmers we have here, clearer).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
you migh wanna do a binary search and keep some parameters until


Keep in mind though, binary search will only be of use with ordered data. At this point, since the poster is not even sure how to find the nearest match, a binary search is probably only going to add unnecessary complication where he has no need of it.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You