Solved

returning index in array

Posted on 2013-12-07
10
269 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
egit plugin on eclipse 8 82
Maven Project: Hibernate Dependencies Conflict 10 38
hibernate jars 4 45
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.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

792 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