searching Arrays. Help wanted. Thanks

Hi there,

I am having a problem searching this array. I am creating a hostel system and I have created several classes. tenant whih has a get room method: (public int getRoom() ; return room), Tenantlist a class based on tenant, hostel, Payment, etc. What I want to do is search if a room is occupied. The code below is part of tenantlist.

public Tenant search(int roomIn)

{

for (int index1=0; index1<maxTenants; index1++)

{

Tenant myTenant= (Tenant)tenants.get(index1);

int room = myTenant.getRoom();

if (room == roomIn)

{

return myTenant;

}

}

returnnull;

}

it searches the rooms entered and if found it brings the record of that tenant. The problem is if the room is not found it just crashes the system witht an error message pointing to this bit of code.

This bit to search for the tenant room does the same. How do i get to say if room not found return null without it giving me an error message.

public Tenant getTenant(int indexIn)

{

return search(indexIn);

}

>

[1719 byte] By [Georgiea] at [2007-11-27 4:18:00]
# 1

Are you sure maxTenants equals the number of elelments in your list? If in the slightest doubt, use tenants.size() instead (I assume tenants is an ArrayList, not an array, correct me if I'm wrong).

What error message(s) do you get?

It's good you've formatted your code in the posting. Please also fix your indentation before posting code.

OleVVa at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 2
the code you posted is not the problem because it will return null when search() returns null. Your problem is where you call getTenant(), that code must be assuming that the returned value is never null and will call some method on the returned object.
gimbal2a at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 3

yes it does. When the room is found it works fine.

If not it brings up the tenantlist and highlights that bit of code. something about null vallue. I did a little test and put if room does not equal roomIn return null. same problem but if I put return myTenant again it does not give me an error. If i try to put any thing else there like return 'room' so it will list all rooms found in index it says expected tenant but found int.

Supose I want it to list all the rooms it found in the index when searching how would i code that?

Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 4

I see two obvious possible reasons for a null value problem in the code you posted:

1. if tenants is null

2. if tenants contains null as an element.

Does it point to a specific line in your code?

"something about null vallue" is rather unspecific. Why don't you quote the message exactly?

All the rooms found, you mean if there are more tenants in a room? The code you've posted will find only one of them. You may want to return a new list contining all the tenants in the room (or an empty list if the room is empty).

Hope this helps.

OleVVa at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 5

The folowing is nonsense:

> All the rooms found, you mean if there are more

> tenants in a room? The code you've posted will find

> only one of them. You may want to return a new list

> contining all the tenants in the room (or an empty

> list if the room is empty).

Your question wasn't clear enough for me to understand it.

OleVVa at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 6
yes at the moment it lists only one. I want to list all rooms occupied. i.e all rooms found in the index whilst searching.
Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 7
Tenant myTenant= (Tenant)tenants.get(index1);This line is highlighted when i enter a room no that i have not put a tenant in. If ihad already put a tenant in it works fine.
Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 8

It certainly can be done.

I'm thinking your design, from what I have a chance to understand of it, doesn't seem to lend itself very well to this problem. You may consider a design with a Room class with an isOccupied() method. Depending, of course, on all your other requirements; only you can tell if it's a good idea after all.

In your current design, run through the tenant list, add room numbers to some collection (you may use Integer objects for room numbers), remove duplicates (if there are more tenants in the same room and you want the room listed only once) and you're done. You may want to use a Set implementation for the room numbers since it ignores duplicates automatically.

OleVVa at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 9

excellent idea. I tried this. But it doesent work. I know its the code, can you help to rephrase it. I thought index1 would list all its gone through and found

public Tenant occupiedRoom(int roomIn)

{

for (int index1=0; index1<maxTenants; index1++)

{

Tenant myTenant= (Tenant)tenants.get(index1);

int room = myTenant.getRoom();

if (room == roomIn)

{

return index1;

}

}

return null;

>

Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 10
am a bit knew at this java code so am finding it difficult sorry
Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 11
hiany ideas
Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 12

> ...

>

> for (int index1=0; index1<maxTenants; index1++)

>{

> Tenant myTenant= (Tenant)tenants.get(index1);

>int room = myTenant.getRoom();

> if (room == roomIn)

>{

> return index1;

You're returning an int instead of a Tenant.

prometheuzza at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 13

The return statement exits from the method immediately (unless inside a try-finally statement), returning the (single) value given after return (if any). So when the first tenant in the room is found, the loop doesn't run any further.

If you want to return a collection containing (potentially) more than object (Tenant, Room, Integer, whatever), you need to create the collection inside the method, put the objects into it in turn, and return the collection after the end of the loop.

HTH

OleVVa at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 14

Hi Thanks very much for all your help.

I thoughtdoing a boolean would solve all my problems. Haha ;-)

This is what I did.

public boolean searchRoom(int roomIn)

{

for (int index1=0; index1<maxTenants; index1++)

{

Tenant myTenant= (Tenant)tenants.get(index1);

int room = myTenant.getRoom();

if (room == roomIn)

{

return true;

}

else

{

return false;

}

the only problem is the bottom bit. It gives me error:else without if. when I add

else

{

if (room != roomIn)

{

return false;

}

}

Doesn't like that either.

When I put on the bottom bit

return true;

}

}

return false;

}

It compiles with no syntax errors. but when search for an existing room its works fine and returns true, but when iI search for a non exixting room it doesn't return false. It just gives an error that the room no was not in the index.

I dont know if its the brackets or what any Ideas on the bottom bit.>

Georgiea at 2007-7-12 9:24:42 > top of Java-index,Java Essentials,Java Programming...
# 15
Again, please fix your indentation, and I will read through your code.The last attempt looks right at first sight. Again, you may want to quote your error message exactly, and we may all learn more.
OleVVa at 2007-7-21 21:03:51 > top of Java-index,Java Essentials,Java Programming...