[Solved] Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \’  \”;  \\ )

“Invalid escape sequence” is most common error at compile time while using Regular Exception or defining File Path while file handling for reading and writing files.

As per JAVA only valid sequence characters are (\b, \t, \n, \f, \r, \”, \’, \). In your code if you are using \ with another character instead of above valid sequence characters then JAVA will throw Compile time error as “Invalid Sequence Characters

Here after showing below Regular Expression and File Path example for throwing will explain about the solution of this.

Regular Expression Example :


package com.test.exceptions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularExpressionTest {

	public static void main(String[] args) {
		String text = "Facing Issues on IT is a site to help others by sharing others experience";
		Pattern pattern = Pattern.compile("\w");
		System.out.println("Saurabh");
		Matcher matcher = pattern.matcher(text);
		if (matcher != null && matcher.find()) {
			for (int i = 0; i<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span><span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span><span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>&lt;matcher.groupCount(); i++) {
				System.out.println(matcher.group(i));
			}
		}
	}
}

Output :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

at com.test.exceptions.RegularExpressionTest.main(RegularExpressionTest.java:10)

File Path Example:

package com.test.exceptions;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class FilePathTest {

	public static void main(String[] args) {
		try
		{
		BufferedReader job = new BufferedReader
	               (new FileReader("F:\\Elasticsearch\FacingIssuesOnIT.txt"));
		}
		catch(FileNotFoundException ex)
		{
			ex.printStackTrace();
		}

	}

}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
	Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )

	at com.test.exceptions.FilePathTest.main(FilePathTest.java:13)

  1. Replace all \ with \\ if using with any other place  instead of valid sequence characters are (\b, \t, \n, \f, \r, \”, \’, \\).
  2. If using eclipse, there is option for setting to automatically add escape chars when pasting text:

Windows -> Preferences ->Java ->Editor -> Typing -> In String Literals ->Escape text when pasting into String literals.

For Example : For above case will change like below

Copy Text :

\w

F:\Elasticsearch\FacingIssuesOnIT.txt

Paste Text :

\w

F:\Elasticsearch\FacingIssuesOnIT.txt

Please below steps for Eclipse Setting for Escape Characters String Literals.

Eclipse Setting for Escape Characters String
Eclipse Setting for Escape Characters String

Summary :

  • Explained cases of exception “Unresolved compilation problem:
    Invalid escape sequence (valid ones are \b \t \n \f \r \” \’ \\ )”
  • Detailed example of Regular Expression and File Path for Invalid escape sequence.
  • Provide Manual solution to fix this compile time issue “Invalid escape sequence”
  • Provide steps to setting escape characters for invalid sequence characters.