Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: My program wont print the sum of the rows in a two-dimensional array

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My program wont print the sum of the rows in a two-dimensional array

    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.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: My program wont print the sum of the rows in a two-dimensional array

    Please format your code using highlight tags, as such:

    [highlight=Java]//your code here[/highlight]

    Unformatted code is very trying to read, especially after a long day of school.

    Could you also please provide an example output and specifically note the code that is responsible for the row addition, as well as the display, so that I can more rapidly troubleshoot?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Problem with Two-Dimensional Array Program
    By soradogoof in forum Collections and Generics
    Replies: 26
    Last Post: February 20th, 2012, 08:17 PM
  2. 2 dimensional array coding
    By Bighairjersey in forum Java Theory & Questions
    Replies: 8
    Last Post: July 22nd, 2011, 02:30 PM
  3. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM
  4. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  5. How can i print rows 1 by 1 using System.out.println?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2010, 07:35 AM