The problem is your changes are not being stored because Strings are immutable. Each of the method calls is returning a new String representing the change, with the current String staying the same. You just need to store the returned String back into the array.

line[i] = line[i].replaceAll("[^a-zA-Z]", "");
line[i] = line[i].toLowerCase();

Because the each method is returning a String you can chain your method calls together. This will perform the second method call on the result of the first, allowing you to do both actions in one line.

line[i] = line[i].replaceAll("[^a-zA-Z]", "").toLowerCase();
Answer from n00begon on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java array › removing all non-alphabetic characters from string array in java
Removing All Non-alphabetic Characters From String Array in Java | Baeldung
November 18, 2024 - In this tutorial, we’ll look at some of the most effective and straightforward methods to remove non-alphabetic characters from strings in an array. We’ll discuss techniques using regular expressions, the Stream API, StringBuilder, and libraries like Apache Commons Lang. Each method will be illustrated with simple code examples and tests. Let’s start with regular expressions to remove all non-alphabetic characters from the String array.
Discussions

How can I remove all Non-Alphabetic characters from a String using Regex in Java - Stack Overflow
Questions title: "How can I remove all Non-Alphabetic characters from a String using Regex in Java" (emphasis added) 2025-01-08T17:10:59.5Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
java - Replacing all non-alphanumeric characters with empty strings - Stack Overflow
Neither should the space at the end of the character class. 2009-11-26T20:31:49.14Z+00:00 ... the reg exp is ok, just remove "/" from the regexp string from value.replaceAll("/[^A-Za-z0-9 ]/", ""); to value.replaceAll("[^A-Za-z0-9 ]", ""); you don't need the "/" inside the regexp, I think you've confused with javascript patterns 2020-05-01T21:06:18.63Z+00:00 ... note that this onl works with Latin alphabet ... More on stackoverflow.com
🌐 stackoverflow.com
6.19 LAB: Remove all non-alphabetic characters Write a program that removes all non-alphabetic characters from the given input. Ex: If the input is: -Hello, 1 worlds! the output is: Helloworld Your program must define and call the following method. The method should return a string representing the input string without non- alphabetic characters public
The method should return a string representing the input string without non- alphabetic characters public static String removeNonAlpha(String userString) 327408, 1874536.23 LAB ACTIVITY 6.19.1: LAB: Remove all non-alphabetic characters 0/10 LabProgram.java Load default template. More on chegg.com
🌐 chegg.com
1
March 20, 2022
Trying to remove non-alphabetical characters from inputted string need help pls
The problem is that you're modifying the string while you're iterating over it. For example, if you have the input string 123, then on the first iteration i=0, so you'll remove the 1, resulting in the string 23. On the second iteration, i=1, and you'll remove the second character of the string which is 3. But you skipped the 2 because it was shifted backwards to an earlier index. You can hack around this, but it's simpler and more efficient to copy the input to a different variable and skip the non-alphabetical characters, instead of removing them in-place. More on reddit.com
🌐 r/learnprogramming
8
0
February 22, 2024
🌐
TutorialsPoint
tutorialspoint.com › article › remove-all-non-alphabetical-characters-of-a-string-in-java
Remove all non-alphabetical characters of a String in Java?
April 22, 2025 - Enter a string: Manisha@#$$342345c h an d String after removing non-alphabetic characters: Manishachand · In Java, a Stream is an abstraction that allows you to process sequences of elements (e.g., collections like lists, sets, arrays, or even I/O resources) in a functional way.
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-all-non-alphabetical-characters-of-a-string-in-java
Remove all non-alphabetical characters of a String in Java - GeeksforGeeks
March 24, 2023 - In this approach, we use the replaceAll() method to replace all non-alphabetic characters with an empty string. The regular expression "[^a-zA-Z]" matches any character that is not an English alphabetical letter (both uppercase and lowercase).
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to remove all non-alphanumeric characters from a string in java?
Remove Non-Alphanumeric Characters from a String in Java
December 19, 2024 - ... How to Remove Non-alphanumeric Characters in Java: Method 1: Using ASCII values Method 2: Using String.replace() Method 3: Using String.replaceAll() and Regular Expression · Alpha stands for alphabets, and numeric stands for a number.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - GeeksforGeeks
July 15, 2025 - It can be punctuation characters ... dollar sign($), equal symbol(=), plus sign(+), apostrophes('). The approach is to use the String.replaceAll method to replace all the non-alphanumeric characters with an empty ...
🌐
Quora
quora.com › How-can-I-remove-all-non-alphanumeric-characters-from-a-string-in-Java
How to remove all non-alphanumeric characters from a string in Java - Quora
Answer: Here you go! Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), ...
Find elsewhere
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › remove-non-alphanumeric-characters-java-string
Java alphanumeric patterns: How to remove non-alphanumeric characters from a Java String | alvinalexander.com
April 18, 2019 - Java String "alphanumeric" tip: How to remove non-alphanumeric characters from a Java String. Here's a sample Java program that shows how you can remove all characters from a Java String other than the alphanumeric characters (i.e., a-Z and 0-9).
🌐
PREP INSTA
prepinsta.com › home › dsa in java › java program to remove characters in a string except alphabets
Remove character in a String except alphabet | PrepInsta
October 13, 2022 - class Main { // function to remove characters and // print new string static void removeSpecialCharacter(String s) { for (int i = 0; i < s.length(); i++) { // Finding the character whose // ASCII value fall under this // range if (s.charAt(i) < 'A' || s.charAt(i) > 'Z' && s.charAt(i) < 'a' || s.charAt(i) > 'z') { // erase function to erase // the character s = s.substring(0, i) + s.substring(i + 1); i--; } } System.out.print(s); } // Driver code public static void main(String[] args) { String s = "$P*r;e..pi, ns'ta^?"; removeSpecialCharacter(s); } } ... import java.util.Scanner; class Main { public static void main(String[] args) { String s = "hel1456lo56wor%^ld"; s=s.replaceAll("[^a-zA-Z]",""); System.out.println(s); } }
🌐
Brainly
brainly.com › engineering › college › how do you remove non-alphabetic characters in java?
[FREE] How do you remove non-alphabetic characters in Java? - brainly.com
By using the replaceAll() method with the correct regex, you can effectively remove unwanted non-alphabetic characters from strings in Java. For instance, if you have the string "Java123!
🌐
YouTube
youtube.com › watch
Remove all non alpha characters Java / How To Tutorial - YouTube
Remove all non alpha charactersWrite a program that removes all non alpha characters from the given input.Ex: If the input is:-Hello, 1 world$!the output is:...
Published   January 16, 2024
🌐
w3resource
w3resource.com › java-exercises › re › java-re-exercise-21.php
Java - Remove all non-alphanumeric characters from a string
March 16, 2026 - public class test { public static void main(String[] args) { String text ="Java Exercises"; System.out.println("Original string: "+text); System.out.println("After removing all non-alphanumeric characters from the said string: "+validate(text)); text ="Ex@^%&%(ercis*&)&es"; System.out.println("\nOriginal string: "+text); System.out.println("After removing all non-alphanumeric characters from the said string: "+validate(text)); } public static String validate(String text) { return text.replaceAll("[^a-zA-Z]", ""); } }
🌐
Reddit
reddit.com › r/learnprogramming › trying to remove non-alphabetical characters from inputted string need help pls
r/learnprogramming on Reddit: Trying to remove non-alphabetical characters from inputted string need help pls
February 22, 2024 -

Hey guys! For class I'm trying to make a program to remove all non-alpha characters from a inputted string. Can you tell me why this isn't working?

For some reason when I input for example "testing test ! bla -o-" it outputs "testingtest!bla-o"

#include <iostream>
using namespace std;
int main() {

string inputStr;
getline(cin, inputStr);
for (int i = 0; i < inputStr.size(); i++){
if (!isalpha(inputStr[i])){
inputStr.replace(i, 1, "");
}
}
cout << inputStr;
return 0;
}

🌐
Brainly
brainly.com › computers and technology › high school › write a program that removes all non-alphabetic characters from the given input. example: if the input is: -hello, 1 world$! the output is: helloworld the program must define and call the following method that takes a string as a parameter and returns the string without any non-alphabetic characters: ```java public static string removenonalpha(string userstring) ``` i need it in java.
[FREE] Write a program that removes all non-alphabetic characters from the given input. Example: If the input - brainly.com
May 26, 2023 - When you run this program, the output will be: Helloworld This is the string with all non-alphabetic characters removed. An example input is "-Hello, 1 world$!", which results in an output of "Helloworld" after removing non-alphabetic characters. The method uses standard Java character checking functions like Character.isLetter, which ensures only alphabetic characters are appended to the result string.
🌐
Experts Exchange
experts-exchange.com › questions › 29246681 › How-to-remove-non-alphanumeric-characters-in-Java.html
Solved: How to remove non-alphanumeric characters in Java? | Experts Exchange
September 16, 2022 - https://www.geeksforgeeks.org/remove-all-occurrences-of-a-character-in-a-string/ then replace the below line ... Select allOpen in new window of course, the variable "c" here should be a string instead of char. ... Here is one way. import java.util.Scanner; public class Remover { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Please type a string."); String input = scanner.nextLine(); System.out.println(input.replaceAll("[^a-zA-Z0-9]", "")); } }