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

Write a Java program that will read in one or more lines of text and then determ

ID: 3767508 • Letter: W

Question

Write a Java program that will read in one or more lines of text and then determine the following:

The number of uppercase letters in the text.

The number of lowercase letters in the text.

The number of digits in the text.

The number of whitespace characters (i.e., blanks or newlines) in the text.

The number of other, non-letter, non-digit, non-whitespace characters in the text.

After reading the text, the program should display the text followed by a display of all of the above statistics.

Suggestions and hints:

Use a String variable to read the text into (since you do not know in advance how much text will be input).

The nextLine() method from Scanner class can be used to read in more than one line into a String variable. To do this, inform user to type multiple lines of text before hitting the “Enter” key on the computer.

You will need to use methods from wrapper class Character.

Explanation / Answer

1) The number of uppercase letters in the text:

Program:

String text = textArea.getText();

  int upperCaseCount = 0;

  for (int i=0; i<text.length(); i++)

{

  for(char c='A'; c<='Z'; c++)

{

  if (text.charAt(i) == c)

{

upperCaseCount++;

}

}

}

2) The number of lowercase letters in the text:

Program:

  String text = textArea.getText();

  int lowerCaseCount = 0;

  for (int i=0; i<text.length(); i++)

{

  for(char c='c'; c<='z'; c++)

{

if (text.charAt(i) == c)

{

  lowerCaseCount++;

}

}

}

3) The number of digits in the text:

Program:

import java.util.Scanner;

  public class CountNumbers

{

  public static int count(int number)

{

int i = 0;

  while(number > 0 )

{

  number = number / 10;

  i ++;

}

System.out.println("The length of the "+ number +" is : " +i);

  return i;

}

public static void main(String args[])

{

int number;

System.out.print("Please enter the number to find the total digits");

Scanner input = new Scanner(System.in);

try

{

number = input.nextInt();

  count(number);

}

  catch(Exception e)

{

  e.printStackTrace();

}

finally

{

input.close();

}

}

}

4) The number of whitespace characters in the text:   

Program:

     public static void main(String[] args)

{

String word = "S N VENKATA RAO";

  String data[];int k=0;

  data=word.split("");

  for(int i=0;i<data.length;i++)

{

if(data[i].equals(" "))

{

  k++;

}

}

  System.out.println(k);

}

}

5) The number of other, non-letter, non-digit, non-whitespace characters in the text:

  Program:

String s = "abcdefà";

  Pattern p = Pattern.compile("[^a-zA-Z0-9]");

  boolean hasSpecialChar = p.matcher(s).find();

  String a = "493284835";

  a.matches("^[0-9]+$");

  

Pattern whitespace = Pattern.compile("\s\s");

  matcher = whitespace.matcher(modLine);

  while (matcher.find()) matcher.replaceAll(" ");

Note : Cause of insufficient time if u combine the above code u will get the last question answer.

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