This is what I need:
In this laboratory you will use a two-dimensional array to tally
the results of a political poll. Krook and Leyer are running for
Mayor of Slimetown. A sample of voters were polled on their
preference. The input file PROG4IN.TXT contains one line for
each voter polled. The line contains the name of the favored
candidate and the age of the voter. You will use an array having
two rows and three columns to tally the voters by favored candidate
and age group. The three age groups are 18-29, 30-49, and 50-99.
You will print a report in the format shown:
Candidate 18-29 30-49 50-99 Total
Krook 2 4 6 12
Leyer 3 3 2 8
And this is what I have:
import java.io.*;
public class Table
{
private int[][] table;
private String[] names;
private int[] rowsum;
int row, col;
public Table() {
table = new int[2][3];
names = new String[2];
names [0] = "Krook";
names [1] = "Leyer";
rowsum = new int[2];
}
public void tally(String name, int age) {
if(name.equals("Krook"))
{
if((age >= 18) && (age <= 29)){
table[0][0] += 1;
}else if((age >= 30) && (age <=49)){
table[0][1] += 1;
}else if((age >= 50) && (age <= 99)){
table[0][2] += 1;
}else{
}
}
if(name.equals("Leyer"))
{
if((age >= 18) && (age <= 29)){
table[1][0] += 1;
}else if((age >= 30) && (age <=49)){
table[1][1] += 1;
}else if((age >= 50) && (age <= 99)){
table[1][2] += 1;
}else{
}
}
for(row = 0; row < 2; row++){
rowsum[row] = 0;
for(col = 0; col < 3; ++col)
rowsum[row] += table[row][col];
}
}
public void report()
{
System.out.printf("Candidate 18-29 30-49 50-99 Total");
System.out.println();
System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(table[0][0] + table[0][1] + table[0][2]);
System.out.println(table[1][0] + table[1][1] + table[1][2]);
System.out.printf("%d%n", rowsum[0]);
System.out.printf("%d%n", rowsum[1]);
}
}
This is main program:
import java.io.*;
import java.util.*;
public class Program4
{
public static void main(String[] args)
{
Scanner infile;
Table table;
String name;
int age;
table = new Table();
try{
infile = new Scanner(new FileReader("PROG4IN.TXT"));
}catch(IOException err){
infile = null;
System.out.println(err);
System.exit(1);
}
while(infile.hasNext()){
name = infile.next();
age = infile.nextInt();
table.tally(name, age);
//table.report();
}
table.report();
infile.close();
}
}
Everytime I run the program, just the labels appears but not the sum of the tables.