Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program (method main) using an array to solve the following problem: •Pr

ID: 3766216 • Letter: W

Question

Write a program (method main) using an array to solve the following problem:
•Prompt the user to enter a String of data
•Use the length() method in order to make your code more scalable (hint research the java API for the String class – it is found in the java.lang package –this is where you will find information for the length() method, as well as the charAt() method).
•Using the charAt(index) method in the String class to access each character in the string individually, produce a count of the number of lower case letters, the count of the number of upper case letters, and a count of the miscellaneous letters.
•In order to produce the count specified previously, there are many ways that this can be achieved – see appendix at end of lab and make your choice.
•Display the total number of letters in the string, and these counts

Example 1: (green is user input)
Enter the phrase:
Linda Crane
The total number of letters is 11
The number of upper case letters is 2
The number of lower case letters is 8
The number of other letters is 1

Example 2:

Enter the phrase:
1234ABCD efgh {}
The total number of letters is 16
The number of upper case letters is 4
The number of lower case letters is 4

Appendix: How to determine which characters are present in the String

NOTE: there are several different ways of solving this problem and for practice sake, I suggest that you try a few.

NOTE: At no time should you have 26 if statements or loops.

NOTE: You can easily google this question – but that won't do you any good on a lab test or your exam if I ask a similar question...

a)Run through each of the characters in the phrase looking for 'a' or 'A', counting how many you have and display the result...then repeat for 'b' or 'B', etc. (Adding to the totals as appropriate).

b)Create an int array of 26 elements to hold the counts of each of the lower case letters in the alphabet and another to hold counts of upper case letters.Then, run through each of the characters in the phrase ONCE, adding 1 to the appropriate element in the array if the character in the phrase is a letter.

c)Use the ASCII code and implement a selection structure.
d)Research the Character class and find appropriate methods.

Explanation / Answer

class Example
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String data=sc.nextLine();
int lengthOfString=data.length();
char ch;
int upper=0,lower=0,others;
for(int i=0;i<length;i++)
{
ch= data.charAt(data[i]);
if(Character.isUpperCase(data[i]))
upper=upper+1;
else if((Character.isUpperCase(data[i])))
lower=lower+1;
else others=others+1;
}

System.out.println(lengthOfString);
System.out.println(upper);
System.out.println(lower);
System.out.println(others);

char arr[]={a,b......z};
int arrCount[26]={0};

for(int i=0;i<arr.length();i++)
{
for(int j=0;j<data.length();j++)
{
   if((arr[i]).equals(data[j]))
   count[i]=count[i]+1;
}
}
//When this calculation is done, the array count will have 26 values, with each value specifying the number of a's, b's and so on :)

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote