how to print the average of each column of 2d n*m array using java

Ads
 

how to print the average of each column of 2d n*m array using java

here is my code:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadContents {
    public static void main(String[] args) throws IOException {
        File file = new File("file1.txt");
        List<float[]> list = new ArrayList<float[]>();
        Scanner scanner = new Scanner(file).useDelimiter("\n");
        while (scanner.hasNext()) {     
            String[] values = scanner.next().trim().split(" "); 
             float[] floats = new float[values.length];
             for (int i = 0; i < values.length; i++) {
                 floats[i] = Float.parseFloat(values[i]);
                 }
                 list.add(floats);
                 } 
                 float[][] values = new float[list.size()][];
                 for (int i = 0; i < list.size(); i++) {
                     values[i] = list.get(i);
                     for (int j = 0; j < values[i].length; j++) {
                         System.out.print(values[i][j] + " ");
                         }
                         System.out.println();
                 }

it prints the content of file.now i want to calculate mean(average) of each column present in the given file having say n*m matrix.
View Answers

May 3, 2011 at 10:47 AM

import java.io.File;
import java.io.IOException;
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class ReadContents {
    public static void main(String[] args) throws IOException {
        float column[] = new float[4];
        File file = new File("rfg.txt"); 
        List<float[]> list = new ArrayList<float[]>(); 
        Scanner scanner = new Scanner(file).useDelimiter("\n");
        while (scanner.hasNext()) {
        String[] values = scanner.next().trim().split(" ");
        float[] floats = new float[values.length];
        for (int i = 0; i < values.length; i++) {
            floats[i] = Float.parseFloat(values[i]); 
            } 
            list.add(floats); 
            }
            float[][] values = new float[list.size()][];
            for (int i = 0; i < list.size(); i++) {
                values[i] = list.get(i);
                for (int j = 0; j < values[i].length; j++) {
                    System.out.print(values[i][j] + " ");
                    column[j] = column[j] + values[i][j];
                    }
                    System.out.println();
                    }
                    for (int i = 0; i < column.length; i++) {
                    System.out.println("Sum of column: "+(i+1)+": "+column[i]);
                    }
    }
}

Ads









Related Tutorials/Questions & Answers:
how to print the average of each column of 2d n*m array using java
using 2D Array
Advertisements
using 2D Array
How to calculate average of array in Java?
array average across column
how to find inverse of n*n 2d array in java
Read text file to 2D array and sorting the second column
game Nim - Java Beginners
Average of Array
How to Print Array in JavaScript
code to display images using 2d array - Swing AWT
Simplest way to print an array in Java
How to using Binary Search Array Java ?
java 2d arrays
how to find subarray from given large 2d array of arbritary dimension
Java 2D
How to add a column with checkboxes for each record in my table in JSP
Storing the Color Image pixel values into 2d array
how to write weighted moving average code using 2d-array
java num
Selecting elements of 2D array with GUI
Version of com.arcticicestudio>northem-java dependency
How to print this in java?
how to check and print the no of times an integer is repeated in an array
how to print the content of file in 2d matrix having same dimension as given in file(n*m).
how to Insert Array Values Into Separate Rows using java?
how to print all colors using awt
how to print - Java Beginners
How to Create a ByteBuffer using Byte Array in java.
how i can print the elements before a spicific value in array queue?
If Sessin Contain Array how can i get each value.

Ads