• 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

string literal is not properly closed by a double quote?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but when I tried putting in double quotes, it said "filepath string cannot be resolved."
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic