Answer:
The solution is given in the explanation section
See comments for detailed explanation of each step
Step-by-step explanation:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
//Prompting the User to enter a String
System.out.println("Enter a sentence or phrase: ");
//Receiving the string entered with the Scanner Object
Scanner in = new Scanner (System.in);
String user_word = in.nextLine();
//OutPuting User entered string
System.out.println("You entered: "+user_word);
//Calling the GetNumOfCharacters Method
System.out.println("Number of characters: "+ GetNumOfCharacters(user_word));
//Calling the OutputWithoutWhitespace Mehtod
System.out.println("String with no whitespace: "+OutputWithoutWhitespace(user_word));
}
//Creating the function GetNumOfCharacters()
//This function will return the number of characters
// in the string entered by the user_word
public static int GetNumOfCharacters (String word) {
int noOfCharacters = 0;
for(int i = 0; i< word.length(); i++){
noOfCharacters++;
}
return noOfCharacters;
}
//Creating the OutputWithoutWhitespace() method
//This method will remove all tabs and spaces from the original string
public static String OutputWithoutWhitespace(String word){
//Use the replaceAll all method of strings to replace all whitespaces with no space.
String new_string = word.replaceAll(" ","");
return new_string;
}
}