I need eclipse code for : Write a program that analyzes text written in the cons
ID: 3761799 • Letter: I
Question
I need eclipse code for :
Write a program that analyzes text written in the console by counting the number of times each of the 26
letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example,
both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must
prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once in the
text, print out the number of times it appeared (and do so in alphabetical order). An effective way to count
characters is to read from the console string-by-string, and loop through all of the characters of each of
these strings. Similar to Problem a, the hasNext() method of the scanner will be useful, and when testing
in Eclipse, press enter and then, once on the empty line, press CTRLD when you are done typing the text.
You must use an array to keep track of how many times each letter is seen in the text. The array should
have 26 elements (one for each letter in the alphabet). Index 0 should be used to track the number of A’s
in the file, index 1 to track the B’s, index 2 to track the C’s, etc., up to index 25 for the Z’s. You could use a
massive if/else block, but the whole reason to use arrays is to make your programs easier. So, instead,
think about how to convert each character you read into the correct index and then increment that value
in the array. For example, if you read an A, then you should increment the value in index 0. Specifically,
you will need to determine if the character is an uppercase letter (between ‘A’ and ‘Z’), a lowercase letter
(between ‘a’ and ‘z’), or something else. If it is a letter, convert it into the appropriate index. Recall that
characters and integers are interchangeable via the ASCII table conversion1. Consider this example to help
get you started:
char input = 'z';
int index = input 'a'; // index equals 25, as 'z' is 122 and 'a' is 97
Explanation / Answer
/**The java program that promps user to enter a text string
* on console.When enter text is complete, press enter and
* press cntl+z (on windows) to stop reading.
* Then prints the count of each small and capital letter
* count on console*/
//LetterCount.java
import java.util.Scanner;
public class LetterCount
{
public static void main(String[] args)
{
//two arrays to count capital and small letters count
int[] capital=new int[26];
int[] small=new int[26];
Scanner scanner=new Scanner(System.in);
String word;
System.out.println("Enter text or press enter and Cntl+Z to stop");
//read until user enter button and cnlt+z
while(scanner.hasNext())
{
//read word by word
word=scanner.next();
for (int i = 0; i < word.length(); i++)
{
char ch=word.charAt(i);
if(isCapital(ch))
{
int index=getCapitalIndex(ch);
//increment the index postion of the capital letter
//array
capital[index]++;
}
else if(isSmall(ch))
{
int index=getSmallIndex(ch);
//increment the index postion of the small letter
//array
small[index]++;
}
}
}
//print letter and count of small letters
System.out.println("Small Letters count");
char letter='a';
for (int i = 0; i < small.length; i++,letter++)
{
System.out.printf("%-4c= %-5d ",letter,small[i]);
}
letter='A';
System.out.println("Capital Letters count");
//print letter and count of capital letters
for (int i = 0; i < small.length; i++,letter++)
{
System.out.printf("%-4c= %-5d ",letter,capital[i]);
}
}//end of main method
//Return index position of small letter
private static int getSmallIndex(char ch)
{
return ch-'a';
}
//Return index position of capital letter
private static int getCapitalIndex(char ch)
{
return ch-'A';
}
//Returns true if ch is capital
private static boolean isCapital(char ch)
{
if(ch>='A' && ch<='Z')
return true;
else
return false;
}
//Returns true if ch is small
private static boolean isSmall(char ch)
{
if(ch>='a' && ch<='z')
return true;
else
return false;
}
}//end of class
-------------------------------------------------------
Sample Output:
Enter text or press enter and Cntl+D to stop
hi hello world
Small Letters count
a = 0
b = 0
c = 0
d = 1
e = 1
f = 0
g = 0
h = 2
i = 1
j = 0
k = 0
l = 3
m = 0
n = 0
o = 2
p = 0
q = 0
r = 1
s = 0
t = 0
u = 0
v = 0
w = 1
x = 0
y = 0
z = 0
Capital Letters count
A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
G = 0
H = 0
I = 0
J = 0
K = 0
L = 0
M = 0
N = 0
O = 0
P = 0
Q = 0
R = 0
S = 0
T = 0
U = 0
V = 0
W = 0
X = 0
Y = 0
Z = 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.