I am new to Java. I have completed some basic in Java. I am stuck in this problem. How can I solve this problem in a simple way? I have seen some solutions but those were hard for me.
You have a guest list of the people who registered for their stay at the hotel. There are 4 rooms in the hotel: 1 single room, 2 double rooms and 1 family room for three guests. The guest list consists of 4 lines. Each line contains the name or the names of the guests registered together in one room.
Read the names of the registered guests from the list and output each name in a separate line and in reverse order starting with the last registered guest. Note that the order of the guests registered in each room is undefined.
well...where exactly are you stuck? Nobody will hand you a solution, but we will help you work through it.
Your best place to start is with pencil and paper. Start writing out - in English - how you would solve this. Write it out step by step. Then, imagine what would happen if you gave those steps to a young child - could then follow them and give you the right answer? If not, refine, clarify, and expand your steps until they can.
Next, think about individual things you need to do. For example, at minimum, you need to be able to
a) read from a file
b) parse each line for name
c) organize the names in order somehow
d) print them out (you missed this in your sample output - it says to print them, then print them in reverse order).
e) print them in reverse order
Focus on one part at a time. I might start with just reading the file, and printing out each line - untouched - just to prove I can read the file. Each of the above steps should probably be its own method.
When i work on part b, i'd start by creating a literal string to parse. This method doesn't care HOW you get the string, just that you HAVE a string. You could even create a bunch of string literals to use for testing...including empty strings, strings with one name, and strings with many names. Think about what you would pass in, and what it should pass back.
and so on...Start with the above. write out your ideas. Start with one tiny piece, write some code, and test it. Then, come back when you have more specific questions.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.