Hi, I wasn't quite sure where to post this but I'm 6 weeks into java so I guessed this would be the best place. My problem is I need a user to be able to search for a type of property such as a Rental, Commercial or a For sale and also within their price range such as £10000, £50,000 etc. My code at the minute is not finding anything and I'm guessing it's because I'm asking it to be equal to two terms in the array rather than one. If I put this line instead of my search returns true but then I'm only searching on the one conditon when I need both to be true otherwise it was redundant asking for their price range if I can't use it.
So in my array, I have a rental for £10,000 at 1,1 and 2,1 I think? , therefore my search should return true as there is a match but I can't work out why it's not doing that.
One value can only be equal to one other value, not two distinct ones. A value cannot be equal to both Rental and 10000.
I'd say your data model is wrong. You probably want to create a special class that contains the property name, type and price. You then loop through an array (or preferably, a List or Set) of instances of this class and look for both:
There is one danger to that code. Line 1 creates a new array of 20 Props references, all of which are null. You then set three of these references to point to actual objects. The remaining 17 are still null. Inside the loop you still try to call methods of the null references. That's going to cause a NullPointerException. You can fix this by adding one extra clause:
This first check ensures that if newprops[i] is null the entire guard will be false, and the rest will not be evaluated.
Joined: May 16, 2011
Ah I see thank you. How would I change it so that it is not a fixed length? I've tried Users newusers = new Users; and Users newusers = new Users; and also Users newusers; but it says the variable is then not initialized. When I set it to null it throws a NullPointerException.
I'm now also having a problem for my users to login since I've changed my model.
What is happening is, if my Username and Password aren't equal to the first entry I have in the login array then it is saying my user is not recognised despite definitely being there. When I remove the break; it then loops though but says originally "not recognised" then "Welcome" then "not recognised" again.