We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,344 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
Page 3 of Article: make my program simple
Hi, I am trying to do a hotel system where i have written up this code, however i was wondering if the there was a way to make the main program simple. So what im trying to do is the code that ‘Views All rooms’ and ‘Adds customer to room’,…

Are you sure you copied what I gave you exactly? i tested the program before posting it to make sure i found all the problems. It should work as is.

also the stack trace doesnt do any good if i dont know where line 44 and 64 are.

DarkLightning7
Junior Poster
111 posts since Jan 2011
Reputation Points: 15
Solved Threads: 9
Skill Endorsements: 0

yea i did.. ill go throught it proper

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

I have got it working thank you..

I just need to work on the other methods. :)

I really appreciate the time u have take out to reply and helped me.

Thank You very much..

I may need some help with the methods like reading from file, storing data in file etc.. but ill work on it and then if i need help ill let u.

Thank you once again

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

I am trying to do the look up guest method but i have a slight problem..

if the guest hasnt checked in it gives ERROR..

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at hotel_program.Main.lookupCustomer(Main.java:91)
    at hotel_program.Main.main(Main.java:43)
Java Result: 1

LINE 91 IS

    System.out.println(names[search]+" has not checked into this hotel");

LINE 43 IS

 lookupCustomer();

it finds the customer to begin.
it works but it just doesnt do what its meants to..

please cn you tell me where i have gone wrong?

public static void lookupCustomer(){            
//Search for a cstomer
             Scanner scan = new Scanner(System.in);
scan.nextLine();
int search=-1;
System.out.print("Enter the name of the guest that is being searched: ");** //I CHANGED THIS SEMI-CLN TO A COLLON IT THEN ALLOWS THE DIFFERNT CUSTOMER SEARCH TO HAPPEN SHOWING DIFFERNT CUSTMERS RATHER THAN THE SAME NAME AGAIN AND AGAIN**
String searchName=scan.nextLine();
for(int x=0;x<names.length;x++)
{   
if(names[x].equalsIgnoreCase(searchName));
{//Every time I search for a guest it says the guest of the first cell is here will explain with output
    search=x;
    break;
}   }                   
if(search==-1)
System.out.println(names[search]+" has not checked into this hotel");
else
   System.out.println(names[search]+" is checked into room "+rooms[search]);

                    }
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

The array index out of bounds error means that your trying to access part of the array that does not exist. so your index is either bigger than the last element in the array (array.length-1) or less than 0. see if you can find where your index is becomming larger than the number of values in the array.

DarkLightning7
Junior Poster
111 posts since Jan 2011
Reputation Points: 15
Solved Threads: 9
Skill Endorsements: 0

I have been trying to research it but i cant seem to fix the error just keep coming up with more errors. :(
Do u know of any site that may help me understand??

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

if you notice in your previous post your allowing search to become negative then using it as an array index. Array indexes can't be negative. They start at 0 and go till the end of the array.

DarkLightning7
Junior Poster
111 posts since Jan 2011
Reputation Points: 15
Solved Threads: 9
Skill Endorsements: 0

I have changed it but now the if else statment is not working accordingly. The search is not working properly..
the results are as follows.

MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: A

Please enter your guest: ALEX
Room: 1


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: A

Please enter your guest: RYAN
Room: 
2


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: V
ROOM            GUEST
1           ALEX
2           RYAN


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: F

Enter the name of the guest that is being searched: RYAN
ALEX is checked into room 1


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: F

Enter the name of the guest that is being searched: ALEX
ALEX is checked into room 1


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: E
BUILD SUCCESSFUL (total time: 51 seconds)
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

here are two problems i noticed with your last code post. If they dont fix your problem go ahead and post your code again so i can look for the problem.

public static void lookupCustomer(){            
    Scanner scan = new Scanner(System.in);
    scan.nextLine();
    int search=-1;

    System.out.print("Enter the name of the guest that is being searched: ");
    String searchName=scan.nextLine();

    for(int x=0;x<names.length;x++){  //just a comment not shure if you know this or not its fine here.  If names last index is 19 names.length will return 20.  be careful.   
        if(names[x].equalsIgnoreCase(searchName));{  // there should not be a semi colin here this is your search problem.
            search=x;
            break;
        }
    }

    if(search==-1)  //checking if search == -1 is fine as long as you dont use it as an array index if it is -1
    System.out.println(names[search]+" has not checked into this hotel"); //this line names[search] should be replaced by searchName
    else
        System.out.println(names[search]+" is checked into room "+rooms[search]);
}
DarkLightning7
Junior Poster
111 posts since Jan 2011
Reputation Points: 15
Solved Threads: 9
Skill Endorsements: 0

Hey sorry about the late reply...

I am still having an error.. the error is within these lines. i tried to change the code around but not successfull.

    if (search==-1)  //checking if search == -1 is fine as long as you dont use it as an array index if it is -1
    System.out.println(names[searchName]+" has not checked into this hotel"); //this line names[search] should be replaced by searchName
    else{
        System.out.println(names[searchName]+" is checked into room "+rooms[search]);
}}

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: int

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

*judging from recent posts

your array expects an integer and searchName is a String

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

i have tried to change it but it still says error :(

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

pLEASE CAN SOME ONE HELP ME FIX THIS CODE.. I JUST DONT UNDERSTAND WHY ITS NOT LOOKING UP THE CUSTOMER WITH THE CORRECT NAME BEING SEARCHED..
THE OUT PUT FOR THIS CODE IS .....

MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: A

Please enter your guest: TOM
Room: 4


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: A

Please enter your guest: PHIL
Room: 3


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: V
ROOM            GUEST
3           PHIL
4           TOM


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: F

Enter the name of the guest that is being searched: TOM
PHIL has not checked into this hotel


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: F

Enter the name of the guest that is being searched: PHIL
PHIL has not checked into this hotel







 public static void lookupCustomer(){            
    Scanner scan = new Scanner(System.in);
    scan.nextLine();
    int search=0;
    System.out.print("Enter the name of the guest that is being searched: ");
    String searchName=scan.nextLine();
    for(int x=0;x<names.length;x++){  //just a comment not shure if you know this or not its fine here.  If names last index is 19 names.length will return 20.  be careful.   
        if(names[x].equalsIgnoreCase(searchName));{  // there should not be a semi colin here this is your search problem.
            search=x;
            break;
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

please can any one help me. how would i change the code to get a string and not a int???

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

anyone help m???

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

ehm ....

int a = 1;
return a+"";
stultuske
Industrious Poster
4,381 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

erm but with my code it wants a interger but i need to change it to an string..

how would i do that

public static void lookupCustomer(){            
    Scanner scan = new Scanner(System.in);
    scan.nextLine();
    int search=0;
    System.out.print("Enter the name of the guest that is being searched: ");
    String searchName=scan.nextLine();
    for(int x=0;x<names.length;x++){   
        if(names[x].equalsIgnoreCase(searchName));{  
            search=x;
            break;
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

your code looks fine to me please post the stack trace and describe the problem. Thanks.

DarkLightning7
Junior Poster
111 posts since Jan 2011
Reputation Points: 15
Solved Threads: 9
Skill Endorsements: 0

THERE IS NO ERROR WITH THE CODE!! BUT THE OUT COMES OUT WRONG..

BELOW I HAVE ADDED 2 CUSTOMERS AND THEN PRESSED "F" TO FIND THE CUSTOMER BUT THE OUTPUT IS WRONG...
IF U LOOK AT THE LAST THRE MENU OUTPUTS U WILL SEE WHAT IM SAYING

MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: A

Please enter your guest: TOM
Room: 2




    MENU 
    A) Add Guest
    V) View All Rooms
    D) Display Empty Rooms
    F) Find Room From Customer Name
    S) Store Program data into file
    L) Load Program from data file
    R) View Rooms Alphabetically
    E) Exit program
    Choice: A

    Please enter your guest: LISA
    Room: 2
    ROOM ALREADY OCCUPIED!
    Reenter room: 4


    MENU 
    A) Add Guest
    V) View All Rooms
    D) Display Empty Rooms
    F) Find Room From Customer Name
    S) Store Program data into file
    L) Load Program from data file
    R) View Rooms Alphabetically
    E) Exit program
    Choice: F

    Enter the name of the guest that is being searched: LISA
    TOM is checked into room 2


    MENU 
    A) Add Guest
    V) View All Rooms
    D) Display Empty Rooms
    F) Find Room From Customer Name
    S) Store Program data into file
    L) Load Program from data file
    R) View Rooms Alphabetically
    E) Exit program
    Choice: F

    Enter the name of the guest that is being searched: TOM
    TOM is checked into room 2


MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: F

Enter the name of the guest that is being searched: PHIL
TOM is checked into room 2
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

i guess no1 can help :( thnks nyway

uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1399 seconds using 2.82MB