Results 1 to 6 of 6
  1. #1
    another_steve is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default return out of scope?

    Hi, I'm writing a method that is suppose to return a boolean but it seems that if the return true/false statements are within the "if statements", then java doesn't realize that it and complains that I am not returning anything.

    Here's a few things I've tried:

    Java Code:
    //in this one, java claims i am not returning anything
    
    private boolean uniqueUsername(String username)
    //checks if username is unique
    {		
    	Vector<String> vUsernames = readAccounts.getUsernames(filename);   
    	    
    	for(int i=0 ; i<vUsernames.size(); i++)
            {
    	    	if ( vUsernames.get(i).equals(username) )
    	    		return false;
    	    	else
    	    		return true;
    	 }
    }
    Java Code:
    //in this one, java always returns true, regardless of the if statements
    
    private boolean uniqueUsername(String username)
    //checks if username is unique
    {		
            uniqueUsername = true;
    
    	Vector<String> vUsernames = readAccounts.getUsernames(filename);   
    	    
    	for(int i=0 ; i<vUsernames.size(); i++)
            {
    	    	if ( vUsernames.get(i).equals(username) )
    	    		uniqueUsername = false;
    	    	else
    	    		uniqueUsername = true;
    	 }
    
            return uniqueUsername;
    }
    Java Code:
    //in this one, java always returns false, regardless of the if statements
    
    private boolean uniqueUsername(String username)
    //checks if username is unique
    {		
            uniqueUsername = false;
    
    	Vector<String> vUsernames = readAccounts.getUsernames(filename);   
    	    
    	for(int i=0 ; i<vUsernames.size(); i++)
            {
    	    	if ( vUsernames.get(i).equals(username) )
    	    		uniqueUsername = false;
    	    	else
    	    		uniqueUsername = true;
    	 }
    
            return uniqueUsername;
    }
    Is there any way i can work around this? Thanks!

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Similar names

    Hello another_steve :D

    Welcome to the forums! You are using variable names that match the method's heading. Also try making the resulting variable local:
    Java Code:
    private boolean uniqueUsername(String username)
    {		
            boolean result = true;
    
    	Vector<String> vUsernames = readAccounts.getUsernames(filename);   
    	    
    	for(int i=0 ; i < vUsernames.size(); i++)
            {
    	    	if (vUsernames.get(i).equals(username) )
    	    		result = false;
    	    	else
    	    		result = true;
    	 }
    
            return result;
    }
    This should do the trick. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    another_steve is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Awesome!! Thanks!!!

  4. #4
    another_steve is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Unfortunately, that didn't solve it. Now it just keeps returning "result" as whichever value is at the end of the "else" statement.

    I'll try some other things and if I find a solution, I will post back.
    Thanks for trying though :)

  5. #5
    another_steve is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    I found a fix for anyone who is interested.

    Java Code:
    	public boolean uniqueUsername(String username)
    	{		
    	    boolean result = true;
    
    		Vector<String> vUsernames = readAccounts.getUsernames(filename);   
    		    
    		for(int i=0 ; i < vUsernames.size(); i++)
    	    {
    		   	if(username.equals(vUsernames.get(i)))
    		   	{
    		    	result = false;
    		   		break;
    		   	}
    		   	else
    		   		result = true;
    		}
    
    	    return result;
    	}
    You need a break statement otherwise the for loop will continue going and will only evaluate the last element in the vector.

    Cheers.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Well done another_steve. :D

    I've had that logical problem many times before. I just did not think of it now. :o
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. How to use session scope in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-31-2008, 10:07 AM
  2. How to use request Scope in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-31-2008, 10:06 AM
  3. How to define Bean Scope
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 10:14 AM
  4. How to use request Scope in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-28-2007, 12:46 PM
  5. How to define Bean Scope
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:43 PM

Posting Permissions

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