1,105,344 Community Members

make my program simple

Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yea i did.. ill go throught it proper

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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]);

                    }
Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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??

Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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)
Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
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]);
}
Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

*judging from recent posts

your array expects an integer and searchName is a String

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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;
Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

anyone help m???

Member Avatar
stultuske
Posting Expert
5,327 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

ehm ....

int a = 1;
return a+"";
Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i guess no1 can help :( thnks nyway

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article