I'm doing a java project at school and one of the tasks that I'm stuck on is correctly calculating and displaying the mean and median of rows and columns of a 2-D array. My code is in classes so I'll try to ask this question as clearly as possible. Here's my code:
//Main class
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) {
FileALL Free = new FileALL("src/com/company/text.txt");
try{
Free.FileRead();
Free.Eraser();
Free.Writing();
Free.Meanie();
Free.Median();
}
catch (Exception e){
e.printStackTrace();
}
}
}
//FILEALL Class (the class with all of the calculations, file I/O, etc.)
package com.company;
import java.io.*;
import java.util.Arrays;
public class FileALL {
//Declare variables
private static int row = 20;
private static int col = 50;
private int i;
private int j;
private int size;
private int Mean;
private int Median;
private int elements;
private static int RawData[][] = new int[row][col];
private File f;
//New method for use in the main class
//Initialize the file
public FileALL (String FileRead) {
f = new File(FileRead);
}
//Method for file input
public void FileRead () {
try {
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = reader.readLine();
while ((line != null)) {
String[] Values = line.trim().split("\\s+");
for (int j = 0; j < Values.length; j++) {
RawData[i][j] = Integer.parseInt(Values[j]);
System.out.print(RawData[i][j] + " ");
}
i++;
System.out.println();
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
System.out.println("ERROR");
System.exit(0);
}
}
//This was a test for file output; I didn't really use it
/*public void FileWrite () throws IOException {
FileInputStream in = null;
FileOutputStream oot = null;
try {
in = new FileInputStream("text.txt");
oot = new FileOutputStream("OG_out.txt");
int c;
while ((c = in.read()) != -1){
oot.write(c);
}
}finally {
if (in != null){
in.close();
}
if (oot != null){
oot.close();
}
}
}*/
//Method for mean calculation
public int Meanie (){
int sum = 0;
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
if (RawData [i][j] > 0){
sum += RawData[i][j];
size++;
}
}
}
if (size != 0) {
Mean = (sum/size);
}
System.out.println("\nThe mean is " + Mean);
return Mean;
}
//Median method
public void Median (){
//Make a separate 1-D array to store the items
int [] list = new int[RawData.length*RawData[0].length];
//Initialize a new integer to keep track of the position of the items in the 1-D array
int Pos = 0;
//Iterating over the 2-D array, now adding in the new list position integer
for (i = 0; i < RawData.length; i++){
for(j = 0; j< RawData.length; j++){
list[Pos++] = RawData[i][j];
}
}
//Sort the 1-D array
Arrays.sort(list);
elements = row - Pos;
System.out.print("\nThe median is: " + MED(list));
}
//Separate method for Median
public double MED(int[]m){
int middle = m.length/2;
if (m.length%2 == 1){
return m[middle];
}
else {
return (m[middle - 1] + m[middle]) / 2;
}
}
//Method for Writing the means, medians, modes, and data sets of the array into a file created specifically for output (rows only)
//Need to figure out how to withhold the zeroes from being printed
public void Writing () throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("src/com/company/OG_out.txt", true));
writer.write("The mean of the input array is " + Mean + "\n\n");
int a = 1;
//I'm using 'i' here because 'i' is my integer for the row counter (for the sake of simplicity)
for (i = 0; i < row; i++){
writer.write("Data set " + a + ": " + Arrays.toString(RawData[i]) + "\n\n==================================================\n\n");
writer.flush();
a++;
}
}
//This is a short method that will erase the data that was written into the output file
public void Eraser () throws IOException {
new BufferedWriter(new FileWriter("src/com/company/OG_out.txt", false));
}
}
//Text.txt file (you can enter any set of numbers in a 2-D array fashion)
1 2 3
5
6
I'm not getting any errors of exceptions but the mean sometimes comes up incorrect (which probably means it's always incorrect) and the median always comes up as 0.0. I'll also need to do the mode of the rows and columns but I don't know where to start (maybe I'll ask a separate question).
Thanks for reading and taking the time to help me out (if you choose to do so).
EDIT: I need a maximum of 20 rows and 50 columns. Also, I got the mean calculation working fine.