Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 23510
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: 12624
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: 23509
47
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: 59115
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
brevity is the soul of wit - shakepeare. Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!