Forums Register Login

Searching a two dimensional array

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi Elaine,
Welcome to JavaRanch!
You want something like
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Okay - so how do I say that in code?
EB
+Pie Number of slices to send: Send
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
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 709 times.
Similar Threads
K&B unclear statement about arrays
two/three dimensional arrays
How to create this String array
Multi Dimensional Arrays
multidimensional array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:15:47.