This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

string literal is not properly closed by a double quote?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm trying to write a program that will allow users to type in a file path and return just the name of the file that they specified. I have the program written out, but every time I try to run it in Eclipse, it gives me the message, "string literal is not properly closed by a double quote." I think it's mostly in reference to the piece of code that says "int lastBackslash = filepathstring.lastIndexOf("\");"

Someone please help me out! Thanks.


import java.util.Scanner;

public class filepath {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a pathname:");
String filepathString = input.next();
int lastBackslash = filepathstring.lastIndexOf("\");
System.out.println(filepathString.substring(lastBackslash + 1));
}
}
 
author
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A backslash is an escape character for literal strings -- so if you want the string to contain a backslash, then you need two backslashes.

Henry
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a back-slash has a special meaning inside double quotes. It (more or less) means "treat the next character as special" - so here java thinks you are trying to put a double quote inside a quoted string. sometimes you need to do that.

To get around this, you have to tell java to treat the backslash in a special way. so, you need TWO backslashes. The first one says "treat the next one special", and so the second is treated as an actual character.
 
Ariel Cotton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but when I tried putting in double quotes, it said "filepath string cannot be resolved."
 
Sheriff
Posts: 23692
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ariel Cotton wrote:Yes, but when I tried putting in double quotes, it said "filepath string cannot be resolved."


Didn't it really say "filepathstring cannot be resolved"? That's what I would expect, since you haven't declared a variable with that name anywhere.

(It's a good idea to copy and paste error messages, rather than trying to retype them. For one thing it's more accurate -- and therefore less confusing -- and for another thing it's faster.)
 
Ariel Cotton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! You were bang-on. I got it to work! Thanks a lot! Next time, I'll copy and paste.
 
Marshal
Posts: 60083
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!