If I type
java DeleteX e < input.txt > output.txt
in the terminal, the code is supposed to remove a certain character (in this case e
) from the input.txt
file and then save the same text to a new file output.txt
but all the e
s should be removed.
Ex: If the text in input.txt
is as follows:
Hello! My name is John Doe.
The output.txt
file should be:
Hllo! My nam is John Do.
But I don't get the spaces in output.txt
. I get:
Hllo!MynamisJohnDo.
Code:
public class DeleteX{
public static void main(String []args){
String x = args[0]; // The character I want removed from the text
char X = x.charAt(0); // Transform the character from String to char
while(! StdIn.isEmpty()){
String line = StdIn.readString(); // The .txt file
for( int i = 0; i < line.length(); i++){
if( line.charAt(i) != X ){
System.out.print(line.charAt(i));
} // if
} // for
} // while
} // main
} // class