• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

...and now for something completely different

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<Joey Dance>
WHOOOOOOOOOOOOOOOOOOOOOO HOOOOOOOOOOOOOOOOOO!!
</Joey Dance>
Thank you to one and all!
EB
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic