File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Searching a multi-dimensional array for two values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching a multi-dimensional array for two values" Watch "Searching a multi-dimensional array for two values" New topic
Author

Searching a multi-dimensional array for two values

Rich Hunt
Greenhorn

Joined: May 16, 2011
Posts: 4
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.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rich Hunt
Greenhorn

Joined: May 16, 2011
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19695
    
  20

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

Joined: May 16, 2011
Posts: 4
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.



fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rich Hunt
Greenhorn

Joined: May 16, 2011
Posts: 4
Thank you. I'll use an ArrayList for both my users and properties.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching a multi-dimensional array for two values