Justin Gauthier
Justin Gauthier

Reputation: 11

I'm getting errors for assignement0

package Assignement0;

import java.util.Scanner;

public class question1 {

    public static void main(String[] args) {

        Scanner keyb = new Scanner(System.in);

        System.out.println("Please enter the amount of rows: ");
        int rows = keyb.nextInt();
        System.out.println("Please enter the amount of columns(planets): ");
        int planets = keyb.nextInt();
        double rain;

        double[][] rainfall = new double[rows][planets];
        System.out.println();
        System.out.println("Data will be entered for 1 row at a time");

        for(int i = 0; i<rainfall.length; i++) {
            //for each column
            System.out.println("Please enter the: "+(i+1)+" value of each planet");
            for(int j = 0; j<rainfall[i].length; j++) {
                //for each row
                rain = keyb.nextDouble();
                while(rain < 0.0) {
                    System.out.println("Please enter a correct value: ");
                    rain = keyb.nextDouble();
                }
                rainfall[i][j] = rain;

            }
        }


        for(int i = 0; i<rainfall.length; i++) {
            for(int j = 0; j<rainfall[i].length; j++) {
                    System.out.print(rainfall[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println(spread(rainfall));
        double[] planetAvg = planetAvg(rainfall, planets, rows);
        System.out.println("The average of each planet: ");
        for(int i = 0; i<planetAvg.length; i++) {
            System.out.print(planetAvg[i]+", ");
        }

        rowTotal(rainfall, rows);
        /*double[] rowTotal =  rowTotal(rainfall, rows);
        System.out.println("The average of each row: ");
        for(int i = 0; i<rowTotal.length; i++) {
            System.out.print(rowTotal[i]+", ");
        }
        */
        double[] maxAndPos = maxAndPos(rainfall);
        System.out.println("The maximum rainfall was: "
                +maxAndPos[0]+"mm, at position: "+maxAndPos[1]+", "+maxAndPos[2]);
    }
    public static double spread(double[][] x) {
        double min = x[0][0];
        double max = x[0][0];
        for(int i = 0; i<x.length; i++) {
            for(int j = 0; j<x[i].length; j++) {
                if(x[i][j]<min)
                    min = x[i][j];
                else if(x[i][j]>max)
                    min = x[i][j];
            }
        }
        return (max-min);
    }
    public static double[] planetAvg(double[][] x, int planets, int row) {
        double[] averages = new double[planets];
        for(int i = 0; i<x.length; i++) {
            for(int j = 0; j<x[i].length; j++) {
                averages[j] += x[i][j];
            }
        }
        for(int i = 0; i<planets; i++) {
        averages[i] /= row;
        }
        return averages;
    }
    public static void rowTotal(double[][] x, int row) {
        double[] rowTotal = new double[row+1];
        for(int i = 0; i<x.length; i++) {
            for(int j = 0; i<x[i].length; j++) {
                rowTotal[i] += x[i][j];
            }
            System.out.println("The average of each row: ");
            for(int w = 0; w<rowTotal.length; w++) {
                System.out.print(rowTotal[i]+", ");
            }
        }
    }
    public static double[] maxAndPos(double[][] x) {
        double[] maxAndPos = new double[3];
        maxAndPos[0] = x[0][0];
        maxAndPos[1] = 0;
        maxAndPos[2] = 0;

        for(int i = 0; i<x.length; i++) {
            for(int j = 0; j<x[i].length; j++) {
                if(x[i][j]>maxAndPos[0]) {
                    maxAndPos[0] = x[i][j];
                    maxAndPos[1] = i;
                    maxAndPos[2] = j;
                }
            }
        }
        return maxAndPos;
    }
}

Output:

Please enter the amount of rows: 
2
Please enter the amount of columns(planets): 
2

Data will be entered for 1 planet at a time
Please enter the: 1 value of each planet
10.1
25.2
Please enter the: 2 value of each planet
3.9
25.1
10.1 25.2 
Exception in thread "main" 3.9 25.1 

-15.000000000000002

The average of each planet: 
7.0, 25.15, java.lang.ArrayIndexOutOfBoundsException: 2
    at Assignement0.question1.rowTotal(question1.java:90)
    at Assignement0.question1.main(question1.java:50)

Upvotes: 1

Views: 41

Answers (3)

Petty
Petty

Reputation: 21

In rowTotal method, correct the inner loop:

for(int i = 0; i<x.length; i++) {
     for(int j = 0; j < x[i].length; j++) {
           rowTotal[i] += x[i][j];
     }
}

Upvotes: 1

Sekhar
Sekhar

Reputation: 61

In rowTotal method

for(int j = 0; j<x[i].length; j++) 

in loop you are checking length with "i" instead of "j". After updating that it's working fine.

    for(int j = 0; j<x[i].length; j++) {
        rowTotal[i] += x[i][j];
    }

Output:

Please enter the amount of rows: 
2
Please enter the amount of columns(planets): 
2

Data will be entered for 1 row at a time
Please enter the: 1 value of each planet
10.1 25.2
Please enter the: 2 value of each planet
3.9 25.1
10.1 25.2 
3.9 25.1 
-15.000000000000002
The average of each planet: 
7.0, 25.15, The average of each row: 
35.3, 35.3, 35.3, The average of each row: 
29.0, 29.0, 29.0, The maximum rainfall was: 25.2 mm, at position: 0.0, 1.0

Upvotes: 1

user12026681
user12026681

Reputation:

at Assignement0.question1.rowTotal(question1.java:90)

is telling you, where the error is going on: in the method rowTotal(). here is your error:

for(int j = 0; j<x[i].length; j++) {
    rowTotal[i] += x[i][j];
}

where you have to replace the i with a j

Upvotes: 1

Related Questions