permaculture playing cards
The moose likes Beginning Java and the fly likes how to return a string in order? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to return a string in order?" Watch "how to return a string in order?" New topic

how to return a string in order?

C Ho

Joined: Dec 15, 2002
Posts: 16
I have a string called schedule, for example:

schedule = "^14%Paul's house?Group Meeting\n^9%Nutty Professor's Office?Office Hours\n^12%MegaBits?Lunch"

After breaking it up using StringTokenizer, how do I return it into chronological order?

Return the schedule for each day of the week as a String with the appointments listed in chronological order, starting with those for the first day of the week and ending with those for the last day of the week.
The String should have the following format
^14%Paul's house?Group Meeting
^9%Nutty Professor's Office?Office Hours

xxx is the String representation of the schedule for each day. There should be no newline after the last schedule.
Note: if there are no appointments scheduled for a certain day, then that day should not be included in the String. (For example, if there are no appointments scheduled on Tuesday then:

should not be included.)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The code that parses the full String could use a StringBuffer to build the final String to return, assuming the input String is in chronological order itself. Your example implied it is, and didn't demonstrate how to show an empty day.

If it's not in order, you can create an intermediate data structure to hold and reorder the appointments. What constitutes an appointment? Write a class to manage that information. You can use the built-in Collection classes (List, Map, Set) to work with your Appointment class.

It will help you more if you try out some techniques, post your code and ask specific questions.
I agree. Here's the link:
subject: how to return a string in order?
It's not a secret anymore!