aspose file tools*
The moose likes Beginning Java and the fly likes ...and now for something completely different Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "...and now for something completely different" Watch "...and now for something completely different" New topic
Author

...and now for something completely different

Elaine Banks
Greenhorn

Joined: Dec 16, 2003
Posts: 18
public class Employee
{
public String ssnToName(String ssn)
{
String employeeName = "";
String [] empArray = {"Mary Lou Buford", "Billy Bob Jackson", "John Doe", "Jane Doe", "Sally Jo Pitkin"} ;
String [] ssnArray = {"111111111", "222222222", "333333333", "444444444", "555555555"};

for (int i=0; i<5; ++i)
{
if (ssnArray[i].equals(ssn))
{
employeeName = empArray[i];
}
else
{
employeeName = "No such employee";
}
}
return employeeName;
} //end of SSNToName
}//end of class
Why does this not work?
EB
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


Why does this not work?

Every time around the loop, one of the two assignments is made -- most of the time, the "no such employee" one. This is a misnomer -- it doesn't mean there's no such employee -- it means that the ssn doesn't match this particular employee, the one at index "i".
When the loop ends, most of the time the string will be "no such employee," as this was the last assignment to execute.


[Jess in Action][AskingGoodQuestions]
Manoj Zachariah
Ranch Hand

Joined: Dec 08, 2003
Posts: 65
when ever a match has been found, quit the loop and display what you want to do.
Elaine Banks
Greenhorn

Joined: Dec 16, 2003
Posts: 18
<Joey Dance>
WHOOOOOOOOOOOOOOOOOOOOOO HOOOOOOOOOOOOOOOOOO!!
</Joey Dance>
Thank you to one and all!
EB
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ...and now for something completely different