Java Reference
In-Depth Information
LISTING 8.2
continued
Enter number 5:
72.404
Enter number 6:
34.8
Enter number 7:
63.41
Enter number 8:
45.55
Enter number 9:
69.0
Enter number 10:
99.18
The numbers in reverse order:
99.18 69.0 45.55 63.41 34.8 72.404 29.06 53.5 48.9 18.36
The
LetterCount
example, shown in Listing 8.3, uses two arrays and a
String
object. The array called
upper
is used to store the number of times each uppercase
alphabetic letter is found in the string. The array called
lower
serves the same
purpose for lowercase letters.
Because there are 26 letters in the English alphabet, both the
upper
and
lower
arrays are declared with 26 elements. Each element contains an integer that is
initially zero by default. The
for
loop scans through the string one character at
a time. The appropriate counter in the appropriate array is incremented for each
character found in the string.
LISTING 8.3
//********************************************************************
//
LetterCount.java Author: Lewis/Loftus
//
//
Demonstrates the relationship between arrays an strings.
//********************************************************************
import
java.util.Scanner;
public class
LetterCount
{
//-----------------------------------------------------------------
//
Reads a sentence from the user and counts the number of
//
uppercase and lowercase letters contained in it.
//-----------------------------------------------------------------
public static void
main (String[] args)
{
final int
NUMCHARS = 26;
Search WWH ::
Custom Search