English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

We 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.
Our “polls.txt” will look like as shown below.

Voteing detail.

RegionPartyAPartyB
NA120002100
NA220001950
NA330001950
NA410001950
NA520001950
NA620001950
NA778002050
NA820008950
NA920001950
NA1020001950
NA1140001950
NA1211003000
NA1335007000
program will read this file, and will read the number of votes for each party for each regain.
After that program will calculate the total number of votes for party and will print the following information on the console.

Total Number of votes obtained by Party A: 23800
Total Number of votes obtained by Party B: 26750
Party B has won by 2950 Votes

I made this but there is some confusion in totaling the number of votes please read the below code and solve this problem.

2006-10-24 14:29:43 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

import java.io.*;
public class Selecter
{
public static void main (String args[ ])
{
FileReader fr = null;
BufferedReader br = null;
try {

fr = new FileReader("polls.txt");
br = new BufferedReader(fr);

String line = br.readLine();

br.close();
fr.close();
}
catch(IOException ioex)
{
System.out.println(ioex);
}
}
}

2006-10-24 14:31:16 · update #1

1 answers

Your String line will contain in the first instance "NA120002100"
You now need to split this string using line.substring method to break this string into its 3 component parts.
The numbers are still Strings at this stage so you need to convert these to a numeric form. As these are votes they will be integers, so use the Integer.parseInt method.
Now you have your integer you can add this to a global int variable to get your total.

2006-10-25 01:01:59 · answer #1 · answered by AnalProgrammer 7 · 1 0

fedest.com, questions and answers