Question

In: Computer Science

5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...

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. */
  
}
}

Solutions

Expert Solution

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:


Related Solutions

6.30 LAB*: Program: Authoring assistant (1) Prompt the user to enter a string of their choosing....
6.30 LAB*: Program: Authoring assistant (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and more...
C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and...
C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...
C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline() function to read a string consisting of white spaces.) Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
(1) Prompt the user for a string that contains two strings separated by a comma.
(In C)(1) Prompt the user for a string that contains two strings separated by a comma. Examples of strings that can be accepted:Jill, AllenJill , AllenJill,AllenEx:Enter input string: Jill, Allen(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. Ex:Enter input string: Jill Allen Error: No comma in string. Enter input string: Jill, Allen(3) Extract the two words from the input string and remove any spaces. Store...
(1) Prompt the user for a string that contains two strings separated by a comma. (1...
(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex: Enter input...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT