Spring Semester 2005
How do we work with strings of characters?
The Stringclass 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.
String:Here's another one:"abc"The following are all examples of"I am a longer string. You have a problem with that?"
Stringof three spaces)
Stringof 2 double quotes)
Stringof 5 backslashes)
Stringcontaining three newlines)
Stringof 3 digits. Looks like a number but isn't.)
+that we use for numbers acts as a concatenation operator for
So the expressionevaluates to"blooming" + "ton"
There is no equivalent of
To store a
Stringvalue you need a
We could use a
Strings from the keyboard.
The fragment above:ConsoleReader c = new ConsoleReader(System.in); String a; a = c.readLine();
- creates a new console reader and names it
- defines a variable
- reads a line from the keyboard and stores the resulting
Stringunder the name of
Next we describe a few operations on
Strings are like
Penguins in how you communicate to them.)
Here are five operations to get us started:
- you can find out the number of characters in the stringThere are 9 characters in
"automaton"their indices are
- you can extract a substring (one method is described here)
"automaton".substring(2,8)has the value (returns the
- you can extract a substring in more than one way (second method is described here)
"automaton".substring(9)has the value
""(returns the empty
- you can extract a single character (you need to know the position of the character in the string)To store it you would need a
"automaton".charAt(0)returns the character
charvariable (of type character).
"a" + "b"evaluates to
'a' + 'b'is a number. Why?
- you can determine if two strings read the same (are equal, so to speak).But
"automaton".equals("automa" + "ton")evaluates to
truewe would have to use
equalsIgnoreCase(can you find it in the API?).
falseyou need a
Thuswill printboolean a; a = 2 < 3; System.out.println(a);
To convert between
Strings and numbers we need to use:
)(described here) converts well-formed
)does the same type of conversion for
Strings for representing floating-point numbers.
To convert numbers into
Strings we concatenate the empty string to them:
When a plus (
1 + ""evaluates to
1.23 + ""evaluates to
+) 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
Stringand a number:
- the number is turned into a
- and then the two
Strings are concatenated.
1 + "2"first becomes
"1" + "2"and then evaluates to
That's whyevaluates to"1" + "1"
"11"and NOT to
That's whyevaluates to"1024".substring(2, 3)
1024.substring(2,3)(without quotes) makes NO sense.
1024.length()(without quotes) makes NO sense.
So now you know how to convert between
Strings and numeric types.
Here are three questions, to make sure you understand how
- what does
1 + 2 + "3"evaluate to?
- what does
(1 + 2) + "3"evaluate to?
- what does
"1" + 2 + 3evaluate to?
- what does
Integer.parseInt("1") + 2 + 3evaluate to?
The answers, in order are:
Here's an extra question:what doesAnswer (in two parts):
'1' + 2evaluate to?
- if we know that characters have a numeric code
'A' + 0evaluates to the code of character
(char)('A' + 3)is the code for
We finish with an example, let's solve this problem:
We'll write this program in class.
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.When the first time is later than the second time:Please enter the first time: 0900 Please enter the second time: 1730 8 hours 30 minutesPlease 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%
Here's a plan of attack:
Write this program (call it
String time1, and
time1split it intoString hour1 = time.substring(0, 2), mins1 = time.substring(2);
time1in minutes:int val1 = hour1 * 60 + mins1;
- Similarly calculate
- Then calculate the difference in minutes between the two:int diff = (val2 + 24 * 60 - val1) % (24 * 60);
diffin hours and minutes:int resH = diff / 60, resM = diff % 60;
MilitaryTime.java) and verify that it works correctly.
Tue Jan 18 14:56:06 EST 2005