• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching a two dimensional array

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
Hi Elaine,
Welcome to JavaRanch!
You want something like
 
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
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
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
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic