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.

alphamonkey
  • 249
  • 5
  • 20
  • `location` is set as long as `candidate` is found in the list. – Thomas Hsieh Apr 10 '15 at 01:54
  • 1
    Are you trying to insert a new value or overwrite an existing one? You should look at this: http://stackoverflow.com/questions/586182/insert-item-into-array-at-a-specific-index – Mark Leiber Apr 10 '15 at 01:55

2 Answers2

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;
    }
}
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Toni
  • 740
  • 2
  • 8
  • 14
  • 1
    You don't need a label to break. Simply use `break;` will do the trick. – Thomas Hsieh Apr 10 '15 at 01:49
  • 1
    @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. – Toni Apr 10 '15 at 01:54
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.

Ungeheuer
  • 1,393
  • 3
  • 17
  • 32