user3105533
user3105533

Reputation: 331

sustitution of '\n' for another character doesn´t work properly

I have the next little code, it has to read a file and when it finds an \n or an \r it substitutes it with a $. The file is this:

for A27y= + 1643.2 - 642 : b
end

F017 = 3.5

and here is the code:

public static void main(String[] args){
        int car;
        String l = "";
        try
        {
            File file = new File("openFile.txt");           
            RandomAccessFile af = new RandomAccessFile(file,"r");           
            while((car = af.read()) != -1)
               l += (char)car;
            l = l.replace('\n', '$').replace('\r', '$');
            System.out.println(""+l);
            af.close();
        }
        catch(IOException ex)
        {
            System.out.println("Cannot open file");
        }
    }

The problem that I have is that every time it finds an '\n' or an '\r' in the file, instead of printing a single $ it prints it double $$, and I don't understand why.

It prints something like this:

for A27y= + 1643.2 - 642 : b$$end$$$$F017 = 3.5$$

when it should be like this:

for A27y= + 1643.2 - 642 : b$end$$F017 = 3.5$

Upvotes: 0

Views: 50

Answers (4)

RealSkeptic
RealSkeptic

Reputation: 34608

The question is what the task actually is.

If it's about finding actual CR and LF characters in the text and replacing each with a dollar sign, then you are getting the correct output. As others have already explained - on Windows each newline is composed of CRLF together.

If it's about finding newlines, and replacing each newline with a dollar sign, then you first need to ask yourself how you define a newline. What if you have a file where there are some separate CRs, some separate LFs and some together? If you want individual CR and LF characters to be replaced with $ but also CRLF to be replaced by a single $, you could try:

        char lastChar = `\0`;
        while((car = af.read()) != -1) {
           char currChar = (char)car;
           if ( currChar == `\n` && lastChar != `\r` || currChar == `\r` ) {
              l += '$';
           } else {
              l += currChar;
           }
           lastChar = currChar;
        }

If, however, you want to replace only what Windows considers a newline (that is, if the file is on Windows, only CRLF are newlines so only they should be replaced with $), then you might change that to:

        char lastChar = `\0`;
        while((car = af.read()) != -1) {
           char currChar = (char)car;
           if ( currChar == `\n` && lastChar == `\r` ) {
              l += '$'
           } else {
              l += currChar;
           }
           lastChar = currChar;
        }

This will keep individual CR and LF characters as they were, and only replace "real newline" combinations.

So choose your definition, and you have your implementation.

Upvotes: 0

InterruptedException
InterruptedException

Reputation: 407

You can try as @Keppil suggested (probadly the "\r\n" is a line feed at your system). I'd suggest making things more efficient and short by not iterating through each character in the file:

String text = readFileAsString("textfile.txt").replace("\n", "$").replace("\r", "");

Upvotes: 0

stegzzz
stegzzz

Reputation: 407

you can check what Keppil said by looking at the values of car in the debugger

Upvotes: 0

Keppil
Keppil

Reputation: 46209

Since some systems (Windows for example) use \r\n to represent end of line, you will sometimes get this behaviour for the code you are showing. You can fix this by first replacing these:

l = l.replace("\r\n", "$").replace('\n', '$').replace('\r', '$');

Upvotes: 3

Related Questions