String to String Comparison being ignored
Hey, new to Java, new to the forums, having some difficulties with comparing strings. (I think).
In one of my classes I'm trying to pull some information from a dynamic file path using an underscore as a reference. This is the relevant function:
savedir is set to something like "M:\Users\LeonsBuddyDave\Documents\My Dropbox\accounts\WI\smith_23456\initialpics"
Code:
private void setNameAndAccount() {
int scorepos, namepos, accpos;
namepos = accpos = scorepos = savedir.indexOf("_");
//Loop through the string to find the start and end points
//of the name and account relative to the underscore
while (savedir.substring(namepos, namepos + 1) != "\\") {
//This loop isn't stopping when it finds a backslash for some reason
namepos -= 1;
}
while (savedir.substring(accpos, accpos + 1) != "\\") {
accpos += 1;
}
//Grab the name and account using the numbers obtained by the loops
name = savedir.substring(namepos+1, scorepos);
account = savedir.substring(scorepos+1, accpos);
}
The code is supposed to find the position of the underscore in the path and use while loops to loop backward and forward to the nearest backslashes (which would then be the start position of the name and the end position of the account number, respectively). The error it gives me is:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
So for some reason, it's ignoring and passing right by the backslashes on its way through the path. Can anyone help me?