Java Reference
In-Depth Information
System.out.printf("Amount due = $%.2f %n",
amountDue);
//Step 6g
break
;
default
:
//Step 7
System.out.println("Invalid customer type.");
}
//end switch
}
}
Sample Run: (In this sample run, the user input is shaded.)
4
This program computes a cable bill.
Enter the account number: 12345
Enter the customer type: R or r (Residential), B or b (Business): b
Enter the number of basic service connections: 16
Enter the number of premium channels: 8
Account number = 12345
Amount due = $520.00
In Java, strings are compared character by character, starting with the first character and
using the Unicode collating sequence. The character-by-character comparison continues
until one of the following three conditions is met: a mismatch is found, the last characters
have been compared and are equal, or one string is exhausted.
For example, the string
"Air"
is less than the string
"Big"
because the first character
'A'
of
"Air"
is less than the first character
'B'
of
"Big"
. The string
"Air"
is less than the
string
"An"
because the first characters of
"Air"
and
"An"
are the same, but the second
character
'i'
of
"Air"
is less than the second character
'n'
of
"An"
. The string
"Hello"
is less than the string
"hello"
because the first character
'H'
of
"Hello"
is
less than the first character
'h'
of
"hello"
.
If two strings of different lengths are compared and the character-by-character compar-
ison is equal through the last character of the shorter string, the shorter string is evaluated
as less than the larger string. For example, the string
"Bill"
is less than the string
"Billy"
and the string
"Sun"
is less than the string
"Sunny"
.
The
class
String
provides the method
compareTo
to compare objects of the
class
String
. The syntax to use the method
compareTo
is:
str1.compareTo(str2)
where
str1
and
str2
are
String
variables. Moreover,
str2
can also be a
String
constant (literal). This expression returns an integer value as follows:
Search WWH ::
Custom Search