Solved

returning index in array

Posted on 2013-12-07
10
270 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Work with App store 7 87
check java version using powershell 13 279
Better way to make a string with template variables in java 3 31
import as existing maven project 3 33
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

679 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