I have a programming assignment which requires the code to be single line(shortest code). I did it, but for some reason, I get the error "Illegal Escape Character". Some people say claim that it will not do the same in eclipse. However, I have to turn in NetBeans project.
This is my code:
public class ShortestCode {
public static void main(String[] args) {
System.out.println(JOptionPane.showInputDialog("Enter your email address").matches(".*@.*\..*") ? "Address Appears Valid" : "Address is Invalid");
}
}
What can I do so it is acceptable by NetBeans IDE? All the error saying is "Illegal escape character." and does not provide with any other hints.
This, this is the problem:
matches(".*@.*\..*")
I suspect you want a regular expression with exactly this pattern:
.*@.*\..*
But you're writing it as a Java string literal, so you need to escape the backslash:
matches(".*@.*\\..*")
The compiler is complaining because \.
isn't a valid escape sequence.
See more on this question at Stackoverflow