Letter counter program. having trouble understanding it.
Hi, I am having trouble understanding this code from my book, it is a simple letter counter that takes in your sentence, then lists the capital letters and lowercase letters in it, including any non alphabetical characters. I have commented the code that I don't understand, any help greatly appreciated. Thank you. Derek
Code:
//********************************************************************
// LetterCount.java Author: Lewis/Loftus
//
// Demonstrates the relationship between arrays and 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;
Scanner scan = new Scanner (System.in);
int[] upper = new int[NUMCHARS];
int[] lower = new int[NUMCHARS];
char current; // the current character being processed
int other = 0; // counter for non-alphabetics
System.out.println ("Enter a sentence:");
String line = scan.nextLine();
// Count the number of each letter occurence
for (int ch = 0; ch < line.length(); ch++)
{
current = line.charAt(ch);
if (current >= 'A' && current <= 'Z')
upper[current-'A']++; [COLOR="green"]//I DON'T UNDERSTAND WHAT CURRENT - A DOES. CAN YOU SUBTRACT FROM LETTERS? AND THE ++?[/COLOR]
else
if (current >= 'a' && current <= 'z')
lower[current-'a']++;[COLOR="green"]//I DON'T UNDERSTAND THIS EITHER, AND THE ++[/COLOR]
else
other++;
}
// Print the results
System.out.println ();
for (int letter=0; letter < upper.length; letter++)
{
System.out.print ( (char) (letter + 'A') );[COLOR="green"]//DONT UNDERSTAND WHAT (CHAR) (LETTER + A) IS DOING[/COLOR]
System.out.print (": " + upper[letter]);
System.out.print ("\t\t" + (char) (letter + 'a') );
System.out.println (": " + lower[letter]);
}
System.out.println ();
System.out.println ("Non-alphabetic characters: " + other);
}
}