In: Computer Science
5.23 LAB: Warm up: Text analyzer & modifier
(1) Prompt the user to enter a string of their choosing. Output
the string. (1 pt)
Ex:
Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself.
(2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)
(3) In main(), call the getNumOfCharacters() method and then output the returned result. (1 pt)
(4) Implement the outputWithoutWhitespace() method, which
outputs the string's characters except for whitespace (spaces,
tabs). Note: A tab is '\t'. Call the outputWithoutWhitespace()
method in main(). (2 pts)
Ex:
Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. Number of characters: 46 String with no whitespace: Theonlythingwehavetofearisfearitself.
Code Template Below:
import java.util.Scanner;
public class TextAnalyzer {
// Method counts the number of characters
public static int getNumOfCharacters(final String usrStr) {
/* Type your code here. */
return 0;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/* Type your code here. */
}
}
PROGRAM: Please find below the detailed commented program and comment if you need help at any line number:
File Name: TextAnalyzer.java
import java.util.Scanner; //import scanner to read user inputs at run time.
public class TextAnalyzer {
// Method counts the number of characters
public static int getNumOfCharacters(final String usrStr) {
int characterCount =0;//set number of characters to 0 initially
for(int i = 0; i < usrStr.length(); i++) {//loop for first character to last character
characterCount++;//increment counter
}//end for
return characterCount;//end final count
}//end method
// Method gives string without white spaces
public static String outputWithoutWhitespace(String usrStr) {
String noSpaceString ="";//set empty string as noSpace String
for(int i = 0; i < usrStr.length(); i++) {//loop for first character to last character
if(usrStr.charAt(i)!=' ') {//if the current character is no a space
noSpaceString= noSpaceString+ usrStr.charAt(i); //add the character from original to new string
}//end if
}//end for
return noSpaceString;
}//end method
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String sentense, noSpaceString;//the string variable to store the phrase
int totalCharacters; //the integer to store total number of characters
System.out.println("Enter a sentence or phrase:");
sentense=scnr.nextLine();//read the phrase
System.out.println("You entered: "+sentense);//print the entered sentence
totalCharacters=getNumOfCharacters(sentense);//pass sentence and get total characters from method
System.out.println("Number of characters: "+totalCharacters); //print number of characters
noSpaceString=outputWithoutWhitespace(sentense);//pass sentence and get same after removing white spaces
System.out.println("String with no whitespace: "+noSpaceString);//print new string without spaces
}//end main
}//end class
SCREENSHOT:
OUTPUT: