University of Toronto - Fall 2000
Department of Computer Science

Week 3 - Strings

String Examples


How to solve Problems

This is what you need to ask yourself:

  1. What am I trying to do?
  2. How do I do this step by step (in English)?
  3. Do I have Java statements to do these steps already?

Example #1 - Drivers' Licence Numbers

Canadian Drivers' licence numbers encode more than you might realize. Consider the licence number "J9854-54467-51223". It must belong to a male born December 23, 1975. Write a program fragment to extract the day, month, and year from the String licence.

	String licence = "J9854-54467-51223"
	String day;
	// you finish this part of the code

	day = licence.substring(15,17);
	String month = licence.substring(13,15);
	String year = licence.substring(10,11).
					concat(licence.substring(12,13));


Example #2 - Class Lists Problem

Class Lists sometimes come in the wrong order. Here is an example:

	Patel, John A.
	Smith, Suzanne
	Van Oak, Bill

But for any given student, I'd like to print his/her given name first, followed by the family name. For example:

    John A. Patel
	Suzanne Smith
	Bill Van Oak

Suppose I could already write code which reads a name from the original backwards class list into a String orig. Finish the program to reverse the order of the name and store the result in String flipped.

int comma = orig.indexOf(",");
String flipped = orig.substring(comma+2,orig.length());
flipped = flipped.concat(" ");
flipped = flipped.concat(flipped.substring(0,comma));