Asked By
lizgriffinn
10 points
N/A
Posted on - 08/01/2011
Question:
You are going to write a small application (“Vote.java”) .This application will help the election department to process the votes through the computer.
You will have the text file “polls.txt” containing the information/details of the number of Votes which each party have through out the country form different parts.
Your “polls.txt” will look like as shown below.
Note: 1. That information in the program is separated by TAB.
2. In the shown file(Fig 1) there are only ten reigns .Your file might have more than ten reigns.
3. The names of the parties are fixed.
You will execute the program like this
Your program will read this file, and will read the number of votes for each party for each region.
After that your program will calculate the total number of votes for party and will print the following information on the console.
1. Total Votes obtained by Party A
2. Total Votes obtained by Party B
3. Name of winning party. Like this “Party A has won by 20036 Votes”
j
Note: In the I have shown only ten records, your program should be able to process the n number (any number) of the regions.
Answered By
James20
5 points
N/A
#96439
Need Java help please provide me its coding
Hi,
Code is here
//Read the comments.
import java.io.* ;
public class Vote
{
public static void main(String args[])
{
int totalPartyA = 0; // Total votes for Party A
int totalPartyB = 0; // Total votes for Party B
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try
{
fileReader = new FileReader(args[0]);
bufferedReader = new BufferedReader(fileReader);
//We will read the header information here, as you can see we are
//Not storing this information any ware because , we do not need it to process
//there had been a requirenment of storing the names of the parties them we might have stored this information too.
String line = bufferedReader.readLine();
line = bufferedReader.readLine();
line = bufferedReader.readLine();
line = bufferedReader.readLine();
// Printing and reading text file
while (line != null)
{
//Splitting on the basis of tab
String votes[] = line.split("t");
// this is altrenative here i have just press tab it is not space,
//which is being used as seprator
//String votes[] = line.split(" ");
//it is good practice that you use "t" because " " will create Ambiguity if some one else will read your code
int voteA = Integer.parseInt(votes[1]);
int voteB = Integer.parseInt(votes[2]);
totalPartyA = totalPartyA + voteA;
totalPartyB = totalPartyB + voteB;
line = bufferedReader.readLine();
}
bufferedReader.close();
bufferedReader.close();
System.out.println("Total Numbed of Votes Obtained by Party A:t"+totalPartyA) ;
System.out.println("Total Numbed of Votes Obtained by Party B:t"+totalPartyB) ;
if (totalPartyA > totalPartyB)
{
System.out.print("Party A has won by ");
System.out.print(totalPartyA-totalPartyB);
System.out.println(" votes");
}
else if (totalPartyB > totalPartyA)
{
System.out.print("Party B has won by ");
System.out.print(totalPartyB-totalPartyA);
System.out.println(" votes");
}
else
{
System.out.println("We Need Re Eelection");
}
}
catch(IOException ioex)
{
System.out.println(ioex);
}
}//end main
}//end class
Save this part as polls.txt
Voteing details.
Region PartyA PartyB
NA1 2000 2100
NA2 2000 1950
NA3 3000 1950
NA4 1000 1950
NA5 2000 1950
NA6 2000 1950
NA7 7800 2050
NA8 2000 8950
NA9 2000 1950
NA10 2000 1950