• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array search code for hotel program  RSS feed

 
Roger Henthorn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking my second class in java right now---last one was a year ago and due to some family circumstances I had to take a bit off from school.... So.. basically ive forgotten much between last year and now--so im reviewing and coding lessons at the same time !

Here is my issue:

This weeks problem has me code a program that asks for a hotel room number (s) with an exit code of -1. After the exit code is presented, the user is then asked for a room to search for. If this room is one that the user entered in the first place it reports the room as occupied. If the room was not entered (not in the array) it then reports as Unoccupied.

It compiles (previously had a break error---break outside of loop issue) and wouldn't compile (you will see where I commented these breaks out for now) Now I have an issue of endless loops..... Occupied, Occupied............. Frustrated!

Any Help would be greatly appreciated!

Here is my code so far:

Edit:
Noticed that some numbers return a loop--- if I enter in 50 and search for 50 it loops. Tired entering the following:

1
56
19
160
34
18
-1
and I search for 160
Returns occupied 6 times, then gives me an out of bounds error... So it seems that there is more wrong than I had originally thought
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. I've added code tags to your post. You can click the edit button to see what they look like. You may also want to read this.

a few comments....

1) Your code isn't properly formatted/indented. See how at the bottom, you have all those closing braces right above each other? That shouldn't happen if you are properly indenting. Proper indentation makes it easier to read and (yes, it's true) spot errors.

2) I personally don't like comments like "end if". and "end main". If your code was properly formatted, it's obvious where those things end.

3) NEVER write this much code at a time. I personally only write 2-3 lines before re-compiling. I fix and and all compiler errors. Then I TEST TEST TEST what I've written, and until I ams ure it works, I don't write another line.
 
Roger Henthorn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is HORRIBLE format---but if I paste it from textpad it comes out just like that---left hand justified.. Is there any way I can turn that off so I can post my code with correct formatting?
 
Roger Henthorn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AHA!!! Got it! Or at least the format for this site-----code is still broken HELP! Seems like this program doesnt like certain values as well...like "47" for instance. If i search for 47 it just returns a blank line---not "unoccupied"

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Henthorn wrote:That is HORRIBLE format---but if I paste it from textpad it comes out just like that---left hand justified.. Is there any way I can turn that off so I can post my code with correct formatting?

You can UseCodeTags (<-click) to preserve formatting when copy and pasting code.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Shall we look at the code rather than the formatting now. It is formatted like that because that is how the editor formatted it. At least you don't appear to have used tabs. I suggest you get a decent text editor as in this link and find out what I mean by writing backwards.

Writing all that code in the main method is poor, non‑object‑oriented design. you should have a room class, which can be occupied or vacant, and a hotel class. The hotel can have an array of rooms.
Searching an array can be as simple as starting from index=0 and going through the array until you find whatever you are looking for.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is implementing a binary search algorithm part of the objective, or are you free to use standard API calls, liie you did with Arrays.sort()?
If you are free to use the standard API I'd use Arrays.binarySearch() to look up the room number.
 
Roger Henthorn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle,

I have to implement a binary search algorithm as part of the objective instead of using API calls.

Looks like the program is working---just not returning PRIME numbers when I search for the rooms.

Issue has to be in my binary search algorithm.....
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sorting the array before searching? Remember that binary search only works reliably on a sorted array.
 
Roger Henthorn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So....

Got some responses back between the Prof. and I and we both agree that my problem lies in my binary search algorithm.... Also need to make the binary search a method so this is what im thinking and what I have now:



This will ask for user input for the occupied hotel rooms---store them in an array and then sort the array using the Arrays.sort api.

The next part I have (this is in a file I named Utility.java)



This allows me to call Utility.binarySearch in my main---however now I'm having issues.... this is what I wanted to do (this is the code I want to have added in my main, right under the sort api call.



My thinking is I ask the user a room to search for---they enter it in and then the program calls the Utility.binarySearch to start searching for that number---im getting an ArrayIndexOutOfBoundsExeption: 4 error when I try to run the program (program lets me enter in the occupied rooms, and prompts me for the room to search for---errors out after that)

Im also kinda stuck (need coffee) about what to do next. I want it to look for that number, and if it finds it simply return the "Room is Occupied"--or if not "Room is Unoccupied". Would really think I could get away with the search setting a value like true or false and if its false print "Unoccupied" else "Occupied"........
 
Roger Henthorn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! it should have been



instead of array, 10, occupiedRoom);


now what do I use for my if/else statement?


?



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!