Results 1 to 5 of 5
- 01-06-2011, 07:30 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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"
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:Java 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); }
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?
- 01-06-2011, 07:40 PM #2
When comparing Objects, == compares the references, not the content. It returns true if two references are pointing to the same instance, regardless of content. This gets convoluted because String literals are stored in the String pool.
So, moral of the story is, you should almost always use .equals(), which compares the actual content of the Object, to compare Strings and Objects.
Edit- You might also look at the other String functions, such as charAt().
Heh. Carrot.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-06-2011, 08:32 PM #3
Now coming to your exception: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Here is where you are trying to fetch the name and account,
This is the signature for substring : java.lang.String.substring(int beginIndex, int endIndex)Java Code:name = savedir.substring(namepos+1, scorepos); account = savedir.substring(scorepos+1, accpos);
What do you think would happen if the beginIndex is greater than the endIndex provided?
Evaluate all your int variable values before passing into the substring method. Try to see if you can find out something?
Hope that helps,
GoldestLast edited by goldest; 01-06-2011 at 08:50 PM.
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-06-2011, 11:25 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Thanks for the help, it's working perfectly now. And thank you for the information regarding Object comparison, that'll save me some pain in the future. xp
- 01-07-2011, 12:25 AM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
why do you need to use a while loop? after you check the last "_" , grab the front part and the back part, then find the last index ( and indexOf) of the slashes. Then get the name/account from there.
Java Code:public class FindNameAndAccount { public static void main(String[] args){ String str = "M:\\Users\\LeonsBuddyDave\\Documents\\My Dropbox\\accounts\\WI\\smith_23456\\initialpics"; int index = str.lastIndexOf("_"); String strName = str.substring(0,index); int indexName = strName.lastIndexOf("\\"); System.out.println( "Name: " + strName.substring(indexName+1) ); String strAccount = str.substring(index+1); int indexAcc = strAccount.indexOf("\\"); System.out.println( "Account: " + strAccount.substring(0,indexAcc)); } }
Similar Threads
-
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
String Comparison
By evant8950 in forum Java AppletsReplies: 6Last Post: 04-22-2009, 08:11 AM -
String comparison
By abhiN in forum New To JavaReplies: 2Last Post: 04-09-2008, 04:47 AM -
String comparison
By sireesha in forum New To JavaReplies: 1Last Post: 12-18-2007, 12:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks