Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 09:43 PM
Member
 
Join Date: Jan 2008
Posts: 4
another_steve is on a distinguished road
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:

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; } }
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; }
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 10:01 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Similar names
Hello another_steve

Welcome to the forums! You are using variable names that match the method's heading. Also try making the resulting variable local:
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 10:17 PM
Member
 
Join Date: Jan 2008
Posts: 4
another_steve is on a distinguished road
Awesome!! Thanks!!!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-28-2008, 11:06 PM
Member
 
Join Date: Jan 2008
Posts: 4
another_steve is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-28-2008, 11:15 PM
Member
 
Join Date: Jan 2008
Posts: 4
another_steve is on a distinguished road
I found a fix for anyone who is interested.

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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-28-2008, 11:34 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Well done another_steve.

I've had that logical problem many times before. I just did not think of it now.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use session scope in Spring Java Tip Java Tips 0 03-31-2008 12:07 PM
How to use request Scope in Spring Java Tip Java Tips 0 03-31-2008 12:06 PM
How to define Bean Scope Java Tip Java Tips 0 03-30-2008 12:14 PM
How to use request Scope in Spring JavaBean Java Tips 0 09-28-2007 02:46 PM
How to define Bean Scope JavaBean Java Tips 0 09-26-2007 10:43 PM


All times are GMT +3. The time now is 01:25 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org