Write a program that removes all spaces from the given input. Ex: If the input is: Hello my name is John. the output is: HellomynameisJohn. Your program must define and call the following method. The method should return a string representing the input string without spaces. public static String removeSpaces (String userString)

Respuesta :

ijeggs

Answer:

public class num2 {

   public static String removeSpaces(String word){

       String wordNoSpaces = word.replaceAll(" ","");

       return wordNoSpaces;

   }

   public static void main(String[] args) {

   String str = "Hello my name is John";

       System.out.println(removeSpaces(str));

   }

}

Explanation:

Using Java Prograamming language

  • Define the method removeSpaces() to accept a string as parameter and return a string as well
  • With the method's body, use Java's built-in method replaceAll() to replace all whitespaces with no spaces String wordNoSpaces = word.replaceAll(" ","");
  • Return the resulting string
  • In the main method define a string with white spaces
  • Call removeSpaces and pass the defined string as an argument
  • Output the result