• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching a multi-dimensional array for two values

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

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:
 
Rich Hunt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks for the welcome and thanks for your help, worked first time. I Created a class like you suggested and was able to loop through each instance no problems to get the desired result.




 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rich Hunt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays have fixed sizes in java. There is nothing you can do about that.

You can, however, use an ArrayList (or some other collection). These will grow as you need them to do so.
 
Rich Hunt
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I'll use an ArrayList for both my users and properties.
 
reply
    Bookmark Topic Watch Topic
  • New Topic