Solved

returning index in array

Posted on 2013-12-07
10
267 Views
Last Modified: 2013-12-08
This is the method I wrote for the object I created "Person." In the directory class I also wrote. Directory d = new Directory(directoryDataFile) I also have an array of person called "people"


 public Person inDirectory(String Lastname)
 {
         for(int k = 0; k < directorySize ; k++)
         {            
         if (Lastname.equals(people[k].getLastname()))
              flag = true;
         return people[k];
         
     }
         return null;
 }

This is a portion of  the main method I'm trying to use it in:

String command = stdin.next();
     
     
if (command.equalsIgnoreCase("find"))

     {
         String Lastname = stdin.next();
           d.inDirectory(Lastname);
     
            if (d.flag = true)
            {
             System.out.println(d.inDirectory(Lastname));
            }
       
            else
            {
             System.out.println(Lastname + " is NOT in the directory");
            }
     }  

My problem is, this keeps returning null instead of saying the the person is in the list. I can't figure out why. Does the excerpts I placed above make sense or do you need more info ?
0
Comment
Question by:aminkeith
  • 5
  • 3
  • 2
10 Comments
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 166 total points
ID: 39703861
You are returning people[0] every time.
You need brackets for the if statement since there are two lines in it.

Also, the way you set the flag is odd. The fact it returns non-null is a better way to check. I wouldn't use that flag at all.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 334 total points
ID: 39704148
As Tommy says, you don't need a flag - just return a Person or null. Also inDirectory implies a boolean method. It would be better to call it findPerson. btw, variable names in Java begin lower case or they just look like class names

You might like to tell us perhaps why this question is entitled 'returning index in array'?
0
 

Author Comment

by:aminkeith
ID: 39704278
Thanks for your input. Also, the question is entitled returning index in an array because people is an array of people objects and I'm trying to get this method to return a particular person object at a particular index number in that array.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39704293
Also, the question is entitled returning index in an array because people is an array of people objects and I'm trying to get this method to return a particular person object at a particular index number in that array.
OK - that's what's confusing me, as it's not returning an index, but a Person
0
 

Author Comment

by:aminkeith
ID: 39704344
See if this clears something up:

 public Person inDirectory(String Lastname)
 {
         for(int k = 0; k < directorySize ; k++)
         {            
         if(Lastname.equals(people[k].getLastname()))
              flag = true;
         return people[k];
         
     }
         return null;
 }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 39704381
See if this clears something up:
That's the same code as before ...
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39704612
Still returning people[0] every time since it's not inside the if.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 334 total points
ID: 39704632
Still returning people[0] every time since it's not inside the if.
(A problem which would disappear if the advice to remove the redundant flag were heeded)
0
 

Author Comment

by:aminkeith
ID: 39704712
thanks guys. I figured it all out. Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39704779
Well it's actually i who assisted, not Tommy. His should be the accepted answer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
Checkbox and ListView in Android Layout 4 56
micro services spring boot application error 3 148
javap not working 8 39
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now