Modify the program below so that the total votes per candidate are shown (i.e.,
ID: 3640630 • Letter: M
Question
Modify the program below so that the total votes per candidate are shown (i.e., add a last row showing total votes received for all three columns of votes).Also, here is a link to the text document being imported: https://skydrive.live.com/redir.aspx?cid=6a7336f2f22f7dcb&resid=6A7336F2F22F7DCB!127&parid=6A7336F2F22F7DCB!117&authkey=!AEHUTN7Rn78A6VM
import java.io.File;
import java.io.IOException;
import java.util.Formatter;
import java.util.Scanner;
public class Program {
public static void main(String[] args) throws IOException {
File election = new File ("voting_2008.txt");
Scanner sc = new Scanner(election);
String[] states = new String[51];
int[][] votes = new int[51][3];
int sum;
for(int s = 0; s < 51; s++)
{
states[s] = sc.nextLine();
}
for(int c = 0; c < 3; c++)
{
for(int s = 0; s < 51; s++)
{
votes[s][c] = sc.nextInt();
}
}
Formatter fmt = new Formatter();
fmt.format("%20s%12s%12s%12s%12s", "State", "Obama", "McCain", "Other", "Total");
System.out.println(fmt);
for(int s = 0; s < 51; s++)
{
fmt = new Formatter();
fmt.format("%20s", states[s]);
System.out.print(fmt);
sum = 0;
for (int c = 0; c < 3; c++)
{
sum += votes[s][c];
fmt = new Formatter();
fmt.format("%12d", votes[s][c]);
System.out.print(fmt);
}
fmt = new Formatter();
fmt.format("%12d", sum);
System.out.print(fmt);
System.out.println();
}
}
}
Explanation / Answer
Hope I got it correct this time.. import java.io.File; import java.io.IOException; import java.util.Formatter; import java.util.Scanner; public class Program { public static void main(String[] args) throws IOException { File election = new File ("voting_2008.txt"); Scanner sc = new Scanner(election); String[] states = new String[51]; int[][] votes = new int[51][3]; int sum; for(int s = 0; s < 51; s++) { states[s] = sc.nextLine(); } for(int c = 0; c < 3; c++) { for(int s = 0; s < 51; s++) { votes[s][c] = sc.nextInt(); } } int totalSum[] = new int[4]; for(int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.