Solved

returning index in array

Posted on 2013-12-07
10
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

628 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