Java Reference
In-Depth Information
if (cNames[mid].equals(name))
found = true ;
else if (cNames[mid].compareTo(name) > 0)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}
Method
processVotes
This method processes the voting data. Clearly, this method must have access to the
arrays candidatesName and votesByRegion , and to the input file voteData.txt .
Thus, this method has three parameters: a parameter to access the input file
voteData.txt , a parameter corresponding to the array candidatesName , and a
parameter corresponding to the array votesByRegion . The definition of
this
method is:
public static void processVotes(Scanner inp,
String[] cNames,
int [][] vbRegion)
{
String candName;
int region;
int noOfVotes;
int loc;
while (inp.hasNext())
{
candName = inp.next();
region = inp.nextInt();
noOfVotes = inp.nextInt();
loc =
binSearch(cNames, candName);
if (loc != -1)
vbRegion[loc][region - 1] =
vbRegion[loc][region - 1] + noOfVotes;
}
}
After processing the voting data, the next step is to calculate the total votes for each
candidate. Suppose that after processing the voting data, the arrays are as shown in
Figure 14-22.
Calculate Total
Votes (Method
addRegionsVote )
 
Search WWH ::




Custom Search