Spring Semester 2005


Lecture Notes Four: Working with Strings. The boolean primitive type.

Strings

How do we work with strings of characters?

The String class is described in the Java API.

A direct link to it is here.

It describes all that needs to be known about Strings of characters.

Here's a String:

"abc"
Here's another one:
"I am a longer string. You have a problem with that?"
The following are all examples of Strings:

" " (a String of three spaces)
"" (the empty String)
"\"\"" (a String of 2 double quotes)
"\\\\\\\\\\" (a String of 5 backslashes)
"\n\n\n" (a String containing three newlines)
"123" (a String of 3 digits. Looks like a number but isn't.)

Concatenating Strings

The + that we use for numbers acts as a concatenation operator for Strings.

So the expression

"blooming" + "ton"
evaluates to "bloomington".

There is no equivalent of -, * or / for Strings.

Reading Strings

To store a String value you need a String variable.

We could use a ConsoleReader to read Strings from the keyboard.

ConsoleReader c = new ConsoleReader(System.in); 
String a; 
a = c.readLine(); 
The fragment above:

Next we describe a few operations on Strings.

(Strings are like Penguins in how you communicate to them.)

What Strings Can Do

Here are five operations to get us started:

Converting Strings into Numbers

To convert between Strings and numbers we need to use:

Converting Numbers into Strings

To convert numbers into Strings we concatenate the empty string to them: When a plus (+) is in between two numbers it adds them up.

When it's in between two Strings it concatenates them.

When it's in between a String and a number:

Thus 1 + "2" first becomes "1" + "2" and then evaluates to "2".

Strings are not Numbers

That's why
"1" + "1"
evaluates to "11" and NOT to 2.

Numbers are not Strings

That's why
"1024".substring(2, 3)
evaluates to "2" but 1024.substring(2,3) (without quotes) makes NO sense.

Likewise

"1024".length()
evaluates to 4, but 1024.length() (without quotes) makes NO sense.

So now you know how to convert between Strings and numeric types.

The Duplicity of +.

Here are three questions, to make sure you understand how + works:

The answers, in order are: "33", "33", "123" and 6.

Characters

Here's an extra question:
what does '1' + 2 evaluate to?
Answer (in two parts):

Also, (char)('A' + 3) is the code for 'D'.

A Complete Example

We finish with an example, let's solve this problem:

15. Write a program that
  • reads two times in military format (0900, 1730) and
  • prints the number of hours and minutes between the two times.

Here is a sample run. User input is in color.

Please enter the first time: 0900
Please enter the second time: 1730
8 hours 30 minutes
When the first time is later than the second time:
Please enter the first time: 1730
Please enter the second time: 0900

15 hours 30 minutes
Here's a sample run with the program:
frilled.cs.indiana.edu%java Fifteen
Please enter the first time: 0920
Please enter the second time: 1025

1 hours 5 minutes
frilled.cs.indiana.edu%java Fifteen
Please enter the first time: 1025
Please enter the second time: 0920
22 hours 55 minutes
frilled.cs.indiana.edu%
We'll write this program in class.

Here's a plan of attack:

  1. Read String time1, and time2.

  2. For time1 split it into
    String hour1 = time.substring(0, 2),
           mins1 = time.substring(2); 
  3. Calculate time1 in minutes:
    int val1 = hour1 * 60 + mins1;
  4. Similarly calculate val2

  5. Then calculate the difference in minutes between the two:
    int diff = (val2 + 24 * 60 - val1) % (24 * 60); 
  6. Transform diff in hours and minutes:
    int resH = diff / 60, 
        resM = diff % 60; 
Write this program (call it MilitaryTime.java) and verify that it works correctly.


Last updated by Adrian German for A201/A597/I210 on Tue Jan 18 14:56:06 EST 2005