• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to accept \ (backslash) in a string when using Scanner.  RSS feed

 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i want to make a very easy user interface for the client. there is a section where they would have to add the path to a file. its targeted at windows users

so lets say they need to specify this path
c:\ program files\Counterstrike\new map"

im making a file in the directory however i would like it if i could just have
Scanner sc = new(Scanner System.in);
String input="";
input=sc.next();

and they could put it in as c:\ program files\Counterstrike\new map
without having to worry about double \\ing to do the escape sequences for java.

any ideas?
 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amaru Shakur wrote:ok i want to make a very easy user interface for the client. there is a section where they would have to add the path to a file. its targeted at windows users

so lets say they need to specify this path
c:\ program files\Counterstrike\new map"

im making a file in the directory however i would like it if i could just have
Scanner sc = new(Scanner System.in);
String input="";
input=sc.next();

and they could put it in as c:\ program files\Counterstrike\new map
without having to worry about double \\ing to do the escape sequences for java.

any ideas?


actually it works im suprised. why is that does the scanner know to catch the escape sequences for me?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a double backslash is only needed in String literals typed in source code. If the String comes from anywhere else* it's not necessary since then it's just the backslash character.


* Ok, java.util.Properties also needs a double backslash, because a backslash is an escape character there too.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amaru Shakur wrote:
Amaru Shakur wrote:
without having to worry about double \\ing to do the escape sequences for java.


actually it works im suprised. why is that does the scanner know to catch the escape sequences for me?


The "\\ing" is required by the compiler in the definition of string literals -- as the backslash has special meaning. It is not needed when string literals are not involved. This means that you don't need it if the string is to be read from a file, read from a network source, or read from standard in via the scanner.

The scanner doesn't know how to catch the escape sequences -- it is just not an issue.

[EDIT: two minutes too slow]

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!