Solved

returning index in array

Posted on 2013-12-07
10
266 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - French characters 6 47
strCopies  challenge 17 74
firstChar challenge 13 86
solarwind tftp server 2 32
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

744 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

14 Experts available now in Live!

Get 1:1 Help Now