-3

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 es 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
RealSkeptic
  • 32,074
  • 7
  • 48
  • 75
Nudd3
  • 29
  • 4

3 Answers3

0

I think a better way to do this is to use the replace method on the string.

like:

line = line.replace(x, "");

and then just print that out.

Mubashar Abbas
  • 4,346
  • 2
  • 32
  • 42
0

you can use String.replace method to replace the character. e.g.

String replaceString=s1.replaceAll(""+x,""); //replaces all occurrences of x to "".

e.g.

String input = "Hello! My name is John Doe.";
char X = 'e';
System.out.println(input.replaceAll("" + X, ""));

The output printed is - Hllo! My nam is John Do.

vlaxmi
  • 438
  • 4
  • 18
  • 1
    What is an "empty char"? – RealSkeptic Mar 28 '17 at 09:58
  • There is no "empty char"... `''`won't compile. You need `String#replace(String, String)` with `""` as second parameter – Stefan Warminski Mar 28 '17 at 10:01
  • I agree that it's easier to use the library implementation of replace than to write your own loop - but if OP uses this approach, they'll get the same result they already do, since their input string will still be missing spaces. – slim Mar 28 '17 at 10:06
  • what if occurrence of x is more than one in given string? – Omore Mar 28 '17 at 10:06
  • @slim str.trim() is also useful in this case. – Omore Mar 28 '17 at 10:35
  • As @MushabarAbbas points out above, `replace(String,String)` *does* replace all instances. Reverted my edit. – slim Mar 28 '17 at 15:28
  • thanks @slim. Not sure why the answer was down voted then – vlaxmi Mar 29 '17 at 00:59
  • @vlaxmi it doesn't actually solve the OP's problem; put `line.replace(...)` into their program instead of the loop, and it would still give a result with no spaces. – slim Mar 29 '17 at 08:03
  • @slim - For this code the result is as expected - String input = "Hello! My name is John Doe."; char X = 'e'; System.out.println(input.replaceAll("" + X, "")); this gives expected result. – vlaxmi Mar 29 '17 at 08:12
  • why was it down voted? the answer is correct as updated on above comment. Also attached output to the answer – vlaxmi Mar 29 '17 at 08:48
  • @vlaximi this isn't OP's problem. If `line == "Hello! My name is John Doe."` OP's loop would write `"Hllo! My nam is John Do."`. OP's problem is (probably) that `line` is `"Hello!"` then `"My"`, `"name"` etc. There is nothing wrong with OP's char-stripping loop, except that it could be replaced with a library method. But OP doesn't seem interested in following up, so this page is effectively dead. – slim Mar 29 '17 at 09:06
0

According to this page http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdIn.html

the StdIn functions operate by reading tokens one at a time, where

A token is a maximal sequence of non-whitespace characters.

That means your call to StdIn.readString(); is not reading a line at a time, but a token (or word) at a time - by definition, without spaces.

So you should read a line at a time instead, instead of using StdIn. See Read input line by line

Community
  • 1
  • 1
racraman
  • 4,410
  • 1
  • 12
  • 15
  • You're probably right, but it's a bit of a guess whether this is the `StdIn` class OP is using. – slim Mar 28 '17 at 10:09