Write a program (method main) using an array to solve the following problem: •Pr
ID: 3766193 • 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
Explanation / Answer
If you have any futher queries, just get back to me.
import java.io.*;
import java.util.*;
class CharactersCount
{
public static void main(String[] args)
{
String string;
System.out.println("Enter the phrase: ");
Scanner sc = new Scanner(System.in);
string = sc.nextLine(); //Reads the phrase.
System.out.println("The total number of letters is: "+string.length()); //Prints the total number of characters.
int upperCaseCount = 0, lowerCaseCount = 0;
for(int i = 0; i < string.length(); i++) //Counts the lowercase and uppercase characters.
if(Character.isUpperCase(string.charAt(i)))
upperCaseCount++;
else if(Character.isLowerCase(string.charAt(i)))
lowerCaseCount++;
System.out.println("The number of upper case letters is: "+upperCaseCount);
System.out.println("The number of lower case letters is: "+lowerCaseCount);
System.out.println("The number of other letters is: "+(string.length()-upperCaseCount-lowerCaseCount));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.