Write a program that removes all non alpha characters from the given input." if input is "-Hello, 1 world$!"
i have been given a task that says "Write a program that removes all non alpha characters from the given input."
if input is "-Hello, 1 world$!"
then output is
Helloworld
I successfully produced code that did that but learned that when testing to successfully complete my task it needs to work with any input. however when it tested with inputs such as
flying
Look! Multiple spaces.
This !$ @ sentence.
it does not work, so thus far i have been adding additional strings to help solve the issue, however when i made the string This !$ @ sentence. work the "-Hello, 1 world$!" no longer works at all. I've tried going through book, googling. what am i doing wrong? i dont understand. I've posted my code bellow
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
String userText2;
String userText3;
String userText4;
userText = scnr.next();
// Your code here to access next element
if (scanner == true){
userText2 = scnr.next();
userText3 = scnr.next();
userText4 = scnr.next();
}
userText = userText.concat(userText2);
userText = userText.concat(userText3);
userText = userText.concat(userText4);
userText = userText.replace("," , "");
userText = userText.replace("$", "");
userText = userText.replace("-" , "");
userText = userText.replace("!" , "");
userText = userText.replace("1" , "");
userText = userText.replace("@" , "");
userText = userText.replace("." , "");
userText = userText.replace(" " , "");
System.out.println(userText);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 5 images