• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic