Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

...and now for something completely different

 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when ever a match has been found, quit the loop and display what you want to do.
 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<Joey Dance>
WHOOOOOOOOOOOOOOOOOOOOOO HOOOOOOOOOOOOOOOOOO!!
</Joey Dance>
Thank you to one and all!
EB
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic