Answer:
See solution below
See comments for explanations
Explanation:
import java.util.*;
class Main {
public static void main(String[] args) {
//PrompT the User to enter a String
System.out.println("Enter a sentence or phrase: ");
//Receiving the string entered with the Scanner Object
Scanner input = new Scanner (System.in);
String string_input = input.nextLine();
//Print out string entered by user
System.out.println("You entered: "+string_input);
//Call the first method (GetNumOfCharacters)
System.out.println("Number of characters: "+ GetNumOfCharacters(string_input));
//Call the second method (OutputWithoutWhitespace)
System.out.println("String with no whitespace: "+OutputWithoutWhitespace(string_input));
}
//Create the method GetNumOfCharacters
public static int GetNumOfCharacters (String word) {
//Variable to hold number of characters
int noOfCharactersCount = 0;
//Use a for loop to iterate the entire string
for(int i = 0; i< word.length(); i++){
//Increase th number of characters each time
noOfCharactersCount++;
}
return noOfCharactersCount;
}
//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
String stringWithoutWhiteSpace = word.replaceAll(" ","");
return stringWithoutWhiteSpace;
}
}