YassVegas
YassVegas

Reputation: 171

Java Invalid escape sequences

I have this error : "Invalid escape sequences (valid ones are \b \t ..." in my code Java.

I make in my code.java :

    ...
    r.exec("cmd /c D:\Doc and Settings\USER\Bureau\Apps-Two.loc.nal");
    ...

The problem is the escapes. How resolve this problem ?

Thank you

Upvotes: 0

Views: 1200

Answers (2)

Anil Reddy Yarragonda
Anil Reddy Yarragonda

Reputation: 767

r.exec("cmd /c D:\Doc and Settings\USER\Bureau\Apps-Two.loc.nal"); // Compiler not able to understand this backslash.

you should use "\\" wherever you want to use actual backslash (\)

change your folder path like this

r.exec("cmd /c D:\\oc and Settings\\USER\\Bureau\\Apps-Two.loc.nal");

See the attached table for your reference

enter image description here

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382150

You just have to escape the escaping character :

r.exec("cmd /c D:\\Doc and Settings\\USER\\Bureau\\Apps-Two.loc.nal");

See Escape Sequences for Character and String Literals :

EscapeSequence:
    \ b    /* \u0008: backspace BS */
    \ t    /* \u0009: horizontal tab HT */
    \ n    /* \u000a: linefeed LF */
    \ f    /* \u000c: form feed FF */
    \ r    /* \u000d: carriage return CR */
    \ "    /* \u0022: double quote " */
    \ '    /* \u0027: single quote ' */
    \ \              /* \u005c: backslash \ */
    OctalEscape        /* \u0000 to \u00ff: from octal value */

Upvotes: 3

Related Questions