• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching a two dimensional array  RSS feed

 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay - I've about had it. In order to thwart my impending self destruction....would someone please post what the code would look like if you have a two dimensional array....like...for instance....

String [] [] empArray =

{ {"111111111", "222222222", "333333333", "444444444", "555555555"},
{"Mary Lou Buford", "Billy Bob Jackson", "John Doe", "Jane Doe", "Sally Jo Pitkin"} };

and you want to pass a social security number (such as the ones listed above) to the function and return the corresponding employees names.
Please make it as plain as possible....
Thanks in advance.
EB
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Elaine,
Welcome to JavaRanch!
You want something like
 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TestEmployee
{
String [] [] empArray;
public void employee ()
{
String [] [] empArray =

{ {"111111111", "222222222", "333333333", "444444444", "555555555"},
{"Mary Lou Buford", "Billy Bob Jackson", "John Doe", "Jane Doe", "Sally Jo Pitkin"} };
}
String SSNToName(String ssn)
{
for (int i=0; i<empArray.length; ++i)
if (empArray[0][i].equals(ssn))
return empArray [1][i];
}
}
So close...and yet so far..... When I try to compile this I get "missing return statement"
What now?
EB
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible to reach the end of the function without finding an answer. In my example code, I show one possible thing you can do at that point, and in a comment, listed two others. You've omitted all three, hence the message. You have to do something if the SSN isn't found.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're only returning in the event that the if statement tests true. What if it's never true? This thought worries the compiler quite a bit and it complains about it. Plus, the compiler is extra worried that the for loop might never run if the entry condition is never true.
Outside of the for loop, you could just add a statement that returns some value that indicates that the search failed, or you could throw an exception. Both solutions should appease the compiler.
 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay - so how do I say that in code?
EB
 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind...you can call off the suicide watch...
Please understand how fried my feeble brain is.
Thank you so much for your help.
Sure to be back,
EB
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!