...

View Full Version : Help with Java Array Assignment



Robertb724
01-24-2012, 08:44 PM
All of the code that counts and displays the letters was not made by me, it is a homework assignment. I have to make it count and display numbers and then count and display the number of spaces. I started to make an array and counting system for the numbers but I can not figure out how to make it print, anyone know how to finish this up?




//********************************************************************
// LetterCount.java Author: Lewis/Loftus/Cocking
//
// 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];
int[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8' , '9'};
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 occurrence
for (int ch = 0; ch < line.length(); ch++)
{
current = line.charAt(ch);
if (current >= 'A' && current <= 'Z')
upper[current-'A']++;
else
if (current >= 'a' && current <= 'z')
lower[current-'a']++;
else
if (current >= '0' && current <= '9')
numbers[current-'0']++;
else
other++;
}
// Print the results
System.out.println();
for (int letter = 0; letter < upper.length; letter++)
{
System.out.print( (char) (letter + 'A') );
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);
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum