|
Spring Semester 2005
|
Strings.
The boolean primitive type.
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.Here's a
String:Here's another one:"abc"The following are all examples of"I am a longer string. You have a problem with that?"Strings:
" "(a Stringof three spaces)""(the empty String)"\"\""(a Stringof 2 double quotes)"\\\\\\\\\\"(a Stringof 5 backslashes)"\n\n\n"(a Stringcontaining three newlines)"123"(a Stringof 3 digits. Looks like a number but isn't.)
The+that we use for numbers acts as a concatenation operator forStrings.So the expression
evaluates to"blooming" + "ton""bloomington".There is no equivalent of
-,*or/forStrings.
To store aStringvalue you need aStringvariable.We could use a
ConsoleReaderto readStrings 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
c- defines a variable
aof typeString- reads a line from the keyboard and stores the resulting
Stringunder the name ofaNext we describe a few operations on
Strings.(
Strings are likePenguins in how you communicate to them.)
Here are five operations to get us started:
- you can find out the number of characters in the string
There are 9 characters in"automaton".length()returns9"automaton"their indices are0through8.
- you can extract a substring (one method is described here)
"automaton".substring(2,8)has the value (returns theString)"tomato"- you can extract a substring in more than one way (second method is described here)
"automaton".substring(9)has the value""(returns the emptyString)- 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'a'charvariable (of type character).Notice that
"a" + "b"evaluates to"ab"but'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 totrue"Automaton".equals("automaton")evaluates tofalse.To get
truewe would have to useequalsIgnoreCase(can you find it in the API?).To store
trueorfalseyou need abooleanvariable.Thus
will printboolean a; a = 2 < 3; System.out.println(a);true.
To convert betweenStrings and numbers we need to use:
Integer.parseInt(java.lang.String)(described here) converts well-formedStrings tointegers.Integer.parseInt("23")evaluates to23Double.parseDouble(java.lang.String)does the same type of conversion forStrings for representing floating-point numbers.Double.parseDouble("23.5")evaluates to23.5
To convert numbers intoStrings we concatenate the empty string to them:When a plus (
1 + ""evaluates to"1"
1.23 + ""evaluates to"1.23"
+) 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:
Thus
- the number is turned into a
Stringfirst,- and then the two
Strings are concatenated.1 + "2"first becomes"1" + "2"and then evaluates to"2".
That's whyevaluates to"1" + "1""11"and NOT to2.
That's whyevaluates to"1024".substring(2, 3)"2"but1024.substring(2,3)(without quotes) makes NO sense.Likewise
evaluates to"1024".length()4, but1024.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+works:
- 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:
"33","33","123"and6.
Here's an extra question:what doesAnswer (in two parts):'1' + 2evaluate to?
- if we know that characters have a numeric code
- then
'A' + 0evaluates to the code of characterA.Also,
(char)('A' + 3)is the code for'D'.
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 minutesHere'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
- Read
String time1, andtime2.
- For
time1split it intoString hour1 = time.substring(0, 2), mins1 = time.substring(2);- Calculate
time1in minutes:int val1 = hour1 * 60 + mins1;- Similarly calculate
val2
- Then calculate the difference in minutes between the two:
int diff = (val2 + 24 * 60 - val1) % (24 * 60);- Transform
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