Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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"

    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);
    	}
    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?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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!

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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,

    Java Code:
    name = savedir.substring(namepos+1, scorepos);
    account = savedir.substring(scorepos+1, accpos);
    This is the signature for substring : java.lang.String.substring(int beginIndex, int endIndex)

    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,

    Goldest
    Last edited by goldest; 01-06-2011 at 08:50 PM.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    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

  1. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. String Comparison
    By evant8950 in forum Java Applets
    Replies: 6
    Last Post: 04-22-2009, 08:11 AM
  4. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 12:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •