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 3 of 3

Thread: Sum elements column by column

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sum elements column by column

    Hi!
    I have to create this program, and it's working fine, but for some reason I cannot comprehend, it doesn't return doubles
    If I try to change:

    double total = 0;

    the return type is still incorrect.

    Any help will be appreciated
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~

    import java.util.Scanner;
    public class SumElementsByColumn {
    public static void main(String[] args) {
    double [][] matrix = new double[3][4];

    Scanner input = new Scanner(System.in);
    System.out.println("Enter " + matrix.length + " rows and " +
    matrix[0].length + " columns: ");
    for (int row = 0; row < matrix.length; row++){
    for ( int column = 0; column < matrix[row].length; column++){
    matrix[row][column] = input.nextDouble();
    }
    }
    sumColumn(matrix,0);
    }
    public static int sumColumn(double[][] m, int columnIndex){
    int total = 0;
    for ( int column = 0; column <= columnIndex; column++){
    total = 0;
    for (int row = 0; row < m.length; row++){
    total += m[row][column];
    }
    System.out.println(total);
    }
    return total;
    }

    }


  2. #2
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Sum elements column by column

    You are accumulating into an int. Total is an int, and the methods return type is int.

    In other words, change the return type to double, and the total into double.
    Last edited by theoriginalanomaly; May 8th, 2013 at 01:30 PM. Reason: Clarity

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sum elements column by column

    Quote Originally Posted by theoriginalanomaly View Post
    You are accumulating into an int. Total is an int, and the methods return type is int.

    In other words, change the return type to double, and the total into double.


    I saw it earlier today!
    Such a silly mistake!!
    Thanks a lot though! n_n

Similar Threads

  1. [SOLVED] sorting only 1 column of 2D array
    By bczm8703 in forum Algorithms & Recursion
    Replies: 5
    Last Post: March 26th, 2013, 10:27 PM
  2. How to get the sum value from a jtable column
    By Asri in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 8th, 2013, 10:00 AM
  3. JTable Column Sizing
    By Jeff.Steinkamp in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2013, 09:23 PM
  4. sum of each row & column in 2-dimensional array.
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 08:09 AM
  5. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM