0

I am making a program for an assignment where I have a list of Candidates (for election), and I am practicing insertion by writing a method to find a specific person in the array and directly before it, insert a new person.

public static void insertCandidate(Candidate[]list , String find, String  candidate, int votes) 
{
int location = 0;
 for (int i =0; i < list.length; i ++){
     String temp = list[i].returnName();
     if (temp.equals(candidate)){
         location = i;

         }
}


 for (int index = list.length - 1; index > location; index --){
        list[index] = list[index - 1];
        }
        list[location ] = new Candidate(candidate, votes);


}
 }

The problem I am having is in the first part, with the for loop. I want the integer location to be the index that the chosen candidate is at, but because location is set inside the for loop, its value stays at 0 for the second for loop. As a result, the person I want to insert is inserted at the very top of the list instead of in front of the person I want.

I'm not sure how to go past this problem, so help would be appreciated.

2
  • location is set as long as candidate is found in the list. Commented Apr 10, 2015 at 1:54
  • 1
    Are you trying to insert a new value or overwrite an existing one? You should look at this: stackoverflow.com/questions/586182/… Commented Apr 10, 2015 at 1:55

2 Answers 2

1

In your first loop where you are checking the candidate name, instead of creating a whole new object, you can do this as returnName() returns a String:

if(list[i].returnName().equals(candidate))
{
    location = i;//saves answer
    break;//gets out of loop
{

I do not see why you would be getting the same value for location unless you are putting in the exact same person and they happen to be at the front of the array. The logic should work, and I have used this same code myself for my classes to find an index, not with string evaluation, but the same logic. In fact, I actually did a very similar assignment around 3 weeks ago.

Also, I suggest that you set location equal to -1 if it is not found in the list as the list does not have negative indexes.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's a snippet of code that should work. I put a label on the first loop and when the position is found, the location is saved and then the first loop is exited:​

int location = 0;
label: for (int i =0; i < list.length; i ++){
    String temp = list[i].returnName();
    if (temp.equals(candidate)){
        location = i;
        break label;
    }
}

2 Comments

You don't need a label to break. Simply use break; will do the trick.
@ThomasHsieh is right, break will do the trick. But if you have multiple loops and it's all getting complicated, it's a good idea to put a label on a loop; so as to be certain what specific loop you are exiting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.