• Post Reply Bookmark Topic Watch Topic
  • New Topic

Illegal escape character while using Path.get() ?  RSS feed

 
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code gives error: illegal escape character because of following code
When I use commented part in above code on line no 12 i.e. codePath.get(/.../..../.../.../); it works well but in JLS it says "The Solaris OS uses the forward slash (/), and Microsoft Windows uses the backslash slash (\)." when I use backslash as I have Microsoft Windows OS 7 it should not give error but it gives and the second pattern works well and when I print using method path.toString(); it prints in proper format i.e. back slash format like C:\Program Files\tooc\selfjava\Input Output\File Operations\Record. why it prints in as mentioned format and won't accept same format in code ?
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change to
"C:\\Program Files\\tooc\\selfjava\\Input Output\\File Operations\\Record"
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Backslash is a special character inside string literals and needs to be escaped.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it worked, where do you read these things?
 
Sheriff
Posts: 4932
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could find it in https://docs.oracle.com/javase/tutorial/java/data/characters.html at the bottom about "Escape Sequences".
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could even try:
 
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The forward-slash path is the OS-neutral syntax (notwithstanding that it's also the native form for the various Unix and Unix-like OS's). Which is why you can define a File with a path string of "C:/Program Files/MyJava/Apps/app1". Even though "C:" is not a legitimate Solaris directory.

I recommend forward-slash notation since it helps make code more portable and you don't have to worry about mis-paired backslashes.

But do realize that no omatter which notation you use, the path rules are CASE_SENSITIVE, even on OS's such as Windows whose paths internally are not case-sensitive. So for best results, use upper and lower-case characters exacrtly as they appear in a disk directory listing.

Incidentally, path separators are an area where portability can suffer. You can obtain the separator character as a system propertly and do brute-force gluing, but here's another way:


 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:I recommend forward-slash notation since it helps make code more portable and you don't have to worry about mis-paired backslashes.

This cannot be thumbs-upped enough.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Vilda thank you, actually I didn't know about JLS documentation before so few beginning chapters I read from other books & now couple of days ago I came to know about JLS so will read whole JLS from 1st chapter but not now as I need to get job first. If you know any good resource for database, GUI(JSwing etc), Servlet, JSP please let me know because from tomorrow these are my next topics to learn.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan. Point jotted down
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pick one path: Swing and Servlets/JSP are completely divergent paths and you should not try to learn them simultaneously.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim for such precious information.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Bear no no I don't mean simultaneously first database( plus SQL simultaneously as I need it in MySQL queries ) then JSwing then next one like that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!