Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to return a string in order?

 
C Ho
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sunday:
^14%Paul's house?Group Meeting
Monday:
^9%Nutty Professor's Office?Office Hours
Tuesday:
^12%MegaBits?Lunch
Wednesday:
xxx
Thursday:
xxx
Friday:
xxx
Saturday:
xxx

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:
Tuesday:
xxx

should not be included.)
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic