6.21 LAB: Remove all non alpha characters Write a program that removes all non alpha characters from the given input. Ex: If the input is: -Hello, 1 world$! the output is: Helloworld 296430.1461558.qx3zqy7 LAB 6.21.1: LAB: Remove all non alpha characters 2/ 10 АCTIVITY
I'm unable to use the method replaceAll because we have not touch upon it yet, so since the chapter cover loops, here are my 2 codes
(Code 1)
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */Scanner scnr = new Scanner(System.in);
String str= scnr.next();
String newStr="";
for (int i=0; i < str.length(); i++) {
if (str.charAt(i) == '@')
newStr+="";
else if (str.charAt(i) == '1')
newStr+="";
else if (str.charAt(i) == '!')
newStr+="";
else if (str.charAt(i) == '-')
newStr+="";
else if (str.charAt(i) == '$')
newStr+="";
else if (str.charAt(i) == '.')
newStr+="";
else if (str.charAt(i) == ' ')
newStr+="";
else
newStr= str.charAt(i);
}
System.out.println(newStr);
}
}
(Code 2)
It's the same as the 1st code but I removed the charAt(i) to newStr as in:
newStr=str;
System.out.println(newStr);
This code displayed the string but no nonalpha characters were removed.



Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images









