the problem is your if statement is never evaluating to true not sure why it looks fine but from what you have told me its not comming out true.

use .trim() on all your strings it might be that there are extra whitespaces.

where is the code that prints the result once your loop finishes searching im guessing this is where the problem is.

also you might want to set search to -1 then if its -1 after search completes you can say the guest has not checked in.

it still doesnt work, it does the same thing.. i give up..
i shall try and do the next task of:

  • Store Program data into file
  • Load Program from data file
  • View Rooms Alphabetically

any help would be nice this project is due tomorrow :(

How can i edit my code so i have only 20 rooms from which can be booked??

and how can i display the empty rooms?

also how would i delete the guest?

You code would be easier to read if you broke it up into smaller methods. I think it is important that loops never be long. If you can't see the top and bottom of the loop at the same time without scrolling, that is a huge warning sign. It's hard to be sure that a loop does everything you need if you can't see it all at once, and even if you can see it all, a long loop can conceal bugs.

Fortunately, long loops can almost always be made shorter by moving the some of the insides to private methods. I think you'll find that your project becomes easier for you if you work on it in small private methods and short loops.

i dont have much time left to complete this otherwise i would

just an update..

if anyone can help i would appreciate it..

I STARTED THIS OFF.. CAN ANYONE HELP ME FINISH IT OFF PLEASE

public static void deleteCustomer(){
        Scanner scan = new Scanner(System.in);
                        System.out.print("Enter the name of the guest checking out: ");

also

i need to define the number of rooms there are to 0-20
1. Store Program data into file
2. Load Program from data file
3. View Rooms Alphabetically

Please someone help :)

bdw i have managed to get the search for a customer working :)

I have added some code to my original code to achieve the following20 rooms in ths hotel
the code i have added i think works slightly but not completey.. the output for the code is 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

DROP
room 0 is empty
room 1 is empty
room 2 is empty
room 3 is empty
room 4 is empty
room 5 is empty
room 6 is empty
room 7 is empty
room 8 is empty
room 9 is empty
Please enter your guest: ALEX
Room: 3
room 0 is empty
room 1 is empty
room 2 is empty
room 3 is empty
room 4 is empty
room 5 is empty
room 6 is empty
room 7 is empty
room 8 is empty
room 9 is empty
Please enter your guest: Room: 

but i what i want is the menu to be to be there once the choice from the menu is selected for eg A add guest ... it shows the drop and then add the customer and room.. then i would like the menu to appear to again so another selection can be madde. I WOULD prefer the empty room code to be in a different method.

pLEASE CAN ANYONE HELP ME TO DO THIS?

THE CODE I HAVE DONE IS     public static void addGuest(){  //if you dont put methodds in a seperate class and instance an object they have to be static to use inside of main.
        Scanner scan = new Scanner(System.in); 
          scan.nextLine();
        String rooms;
int roomNum = 0;
String[] hotel = new String[10];
for (int x = 0; x < 10; x++ ) hotel[x] = ""; //initialise
drop(hotel); //better to initialise in a procedure
while ( roomNum < 10 )
{
for (int x = 0; x < 10; x++ )
{
if (hotel[x].equals("e"))System.out.println("room " + x + " is empty");
}
        System.out.print("Please enter your guest: ");
        String guest=scan.nextLine();
        names=addName(guest,names);
        System.out.print("Room: ");
        int room=scan.nextInt();

}}
         private static void drop( String hotelRef[] ) {
for (int x = 0; x < 10; x++ ) hotelRef[x] = "e";
System.out.println( "DROP");
}

I wrote this code thinkin i had to order the rooms but i have to do them as alphabetical order.
I have tried to change the code but its not giving the correct output.
please can someone help me so this does alphabetical order rather than sorting in asending order.
the code is

  for(int x=0;x<rooms.length;x++)
    {
        for(int y=0;y<rooms.length;y++)
        {
            if(rooms[x]<rooms[y])
            {
                int dummy=rooms[x];
                String dummy2=names[y];
                rooms[x]=rooms[y];
                names[x]=names[y];
                rooms[y]=dummy;
                names[y]=dummy2;
            }
        }
    }
}

PLEASE Can anyone help me to add a loop to this code so it prints out this switch case statement in to a file?

The code i have written makes the files and adds a sting but i need the data from the output to be in the file?

        try {

 String content = "This is the content to write into file";

            File file = new File("/users/blabla/filename.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The output code which i want it to print is `

 public static void addGuest(){  
        Scanner scan = new Scanner(System.in); 
        scan.nextLine();
        System.out.print("Please enter your guest: ");
        String guest=scan.nextLine();
        names=addName(guest,names);
        System.out.print("Room: ");
        int room=scan.nextInt();
        rooms=addRoom(room,rooms);

You can start writing to a java.io.PrintWriter parameter in your methods that you want to write things. So change addGuest() to addGuest(java.io.PrintWriter out) and instead of bw.write(content) you can call addGuest(new java.io.PrintWriter(bw)). You may need to change addName and addRoom so they also take a PrintWriter if they need to print things.

do u know how a java console application is different to a windows application?

I have an error
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>

the line with the error is ...

 case 'A':
                    addGuest(java.io.PrintWriter out);

A windows application is an applicaiton that opens at least one window. A console application is an application that opens no windows, and therefore usually writes to System.out or to a file.

I meant for you to change the definition of addGuest() to addGuest(java.io.PrintWriter out). I didn't mean for you make that change every place where addGuest() is called. addGuest(java.io.PrintWriter out) is not a legal method call.

okay so i did what u said but i get his erroe..

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   variable bw

i have fixed that but nothing prints in the file :(

I can't guess what you did, but in the source code you showed a few posts ago you clearly had a local variable bw declared on line 13. You must have done something odd if that variable can't be found.

i have fixed that now.. but it still doesnt print anything to the file?

Since you did something odd to cause the problem, I can only guess that you also did something odd to fix the problem. It was probably a mistake. Unless you want to share some more source code, there's not much more anyone can say.

well here is the code to write the file

 try {



            File file = new File("/Users/blabla/filename.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            addGuest();
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
        }
    }}

the code to addGuest

   private static void addGuest() {
        Scanner scan = new Scanner(System.in); 
        scan.nextLine();

and here the imports

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

I am now wondering if I may have misunderstood what you are trying to achieve. You said that "the output code which [you] want it to print is" the addGuest method. My first thought was that you wanted to print the source code of the method to the file, because that is what printing the code normally means, but I discarded that as unlikely.

Next I noticed that addGuest outputs some things to System.out, so I assumed that you wanted those outputs to go to the file, but now I notice that those outputs are clearly designed to be interactive and probably aren't what you really want to appear in the file.

Unless you explain what you really want to appear in the file, all I can offer to help you is that a java.io.PrintWriter is a good tool for creating text files of all sorts of content. Just use a PrintWriter like you would use System.out to print whatever you want into the file.

The user selects the option to add the guest, they write the name and room number down.
What i want is once they have written the name and room number down to STORE in file and then to be able to LOAD the data from the file.
I need the STORE and Load be in seperate methods.

for example: i press "S" it stores the data and when i press "L" it loads the data from file.

If you have the information that you want to save to a file, then it is just a matter of writing that information one piece at a time in a certain order that you choose. The best way to do that depends on whether you want the file to be a text file that you can examine in a text editor, or a binary file that only your application can read. The first option makes it easier to see that your application is working correctly, but it is more difficult.

To save to a text file, create a java.io.PrintWriter for that file and iterate through the things you want to save, printing each thing as you want it to appear in the file. Your choice of how you want it to appear is very important because you will need to be able to read it back in later. When you want to read it in, I recommend that you create a java.util.Scanner for the file and use its many convenient methods to reconstruct everything that you saved.

If a binary file is good enough for you, then you can do this all much more easily using java.io.DataOutputStream and java.io.DataInputStream. This way you are saved from the trouble of having to worry about whitespace and parsing human-readable text. You just output the primitives that you want to save to a file, and then later read those primitives back in the same order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.