-1

NEW EDIT

beginner Java student here. I have an assignment to do a simple Java console application to monitor food consumption of a family of four monkeys. The application requirements are:

2 dimensional 4x7 array; 4 monkeys, 7 days of the week

Average amount of food eaten daily by the whole family of monkeys.

Average amount of food eaten weekly by each monkey.

The least daily amount of food eaten during the week by any one monkey.

The greatest daily amount of food eaten during the week by any one monkey.

No negative numbers.

User input between 1 and 50 pounds.

So far, I have the average weekly food consumption, most and least food consumption by an individual monkey sections working. I will work on the rest as I get to them. I want to keep the code as simple as possible.

However, I am stuck at the user input validation section. I want to be able to validate against anything other than a positive integer between 1 and 50. When the application is run, the compiler puts out "cannot find symbol: variable col. Col is the column in the array. I am not sure what I'm doing wrong. I'm pretty sure it is something very simple, but I am hitting a wall as I can't see what that may be.

Can anyone help show me what I am doing wrong or point me in the right direction? Thank you so much!

*****************************New edit: ***********************************

I have the new code that works but the weekly average consumption for each monkey is incorrect. Logic seems to be ok it appears. What can I do to correct this?

Thank you.

New code:

package monkeybusiness;

import java.util.Scanner;
import java.util.InputMismatchException;

public class MonkeyBusiness {

    public static void main(String[] args) {

        System.out.println("Welcome to MonkeyBusiness!");
        System.out.println("Feed Four Monkeys.\n");

        // Array - four monkeys (row) and seven days (col)
        int monkeyarray[][] = new int[4][7];

        //User input and validation
        Scanner scanner = new Scanner(System.in);

        for (int row = 0; row < 4; row++) {

            for (int col = 0; col < 7; col++) {

                boolean inputValidate = false;

                int number = 0;

                do {

                    System.out.println("Enter food amount (1-50) for Monkeys "
                            + (row + 1) + " on Day " + (col + 1));

                    try {

                        number = scanner.nextInt();

                        if (number >= 1 && number <= 50) {

                            inputValidate = true;

                        } else {

                            System.out.println("Entry not in range. ");

                            scanner.nextLine();

                        }

                    } catch (InputMismatchException exception) {

                        System.out.println("Entry not a number.");

                        scanner.nextLine();

                    }

                } while (!(inputValidate));

                monkeyarray[row][col] = number;
            }
        }

        // Calculate total daily average for family
        int average;

        int[] perday = new int[]{0, 0, 0, 0, 0, 0, 0};

        for (int col = 0; col < 7; col++) {
            for (int row = 0; row < 4; row++) {
                perday[col] += monkeyarray[row][col];
            }
        }

        int sum = 0;

        for (int row = 0; row < 7; row++) {
            sum += perday[row];
        }

        average = sum / 28;
        System.out.println("");
        System.out.println("*************************************************");
        System.out.println("Daily average amount for "
                + "whole family is: " + average);

        // Calculate average weekly amount of food for each monkey
        // Monkey# 1
        int avg1;
        int sum1 = 0;

        int[] perweek1 = new int[]{0, 0, 0, 0, 0, 0, 0};

        for (int col = 0; col < 7; col++) {
            for (int row = 0; row < 4; row++) {
                perweek1[col] += monkeyarray[row][col];
            }
        }

        for (int row = 0; row < 7; row++) {
            sum1 += perweek1[row];
        }
        System.out.println("Monkey# 1: " + sum1);

        avg1 = sum1 / 7;

        System.out.println("Weekly average amount for "
                + "Monkey# 1 is: " + avg1);

        // Monkey# 2
        int avg2;
        int sum2 = 0;

        int[] perweek2 = new int[]{0, 0, 0, 0, 0, 0, 0};

        for (int col = 0; col < 7; col++) {
            for (int row = 0; row < 4; row++) {
                perweek2[col] += monkeyarray[row][col];
            }
        }

        for (int row = 0; row < 7; row++) {
            sum2 += perweek2[row];
        }

        System.out.println("Monkey# 2: " + sum2);

        avg2 = sum2 / 7;

        System.out.println("Weekly average amount for "
                + "Monkey# 2 is: " + avg2);

        // Monkey# 3
        int avg3;
        int sum3 = 0;

        int[] perweek3 = new int[]{0, 0, 0, 0, 0, 0, 0};

        for (int col = 0; col < 7; col++) {
            for (int row = 0; row < 4; row++) {
                perweek3[col] += monkeyarray[row][col];
            }
        }

        for (int row = 0; row < 7; row++) {
            sum3 += perweek3[row];
        }

        System.out.println("Monkey# 3: " + sum3);

        avg3 = sum3 / 7;

        System.out.println("Weekly average amount for "
                + "Monkey# 3 is: " + avg3);

        // Monkey# 4
        int avg4;
        int sum4 = 0;

        int[] perweek4 = new int[]{0, 0, 0, 0, 0, 0, 0};

        for (int col = 0; col < 7; col++) {
            for (int row = 0; row < 4; row++) {
                perweek4[col] += monkeyarray[row][col];
            }
        }

        for (int row = 0; row < 7; row++) {
            sum4 += perweek4[row];
        }

        System.out.println("Monkey# 4: " + sum4);

        avg4 = sum4 / 7;

        System.out.println("Weekly average amount for "
                + "Monkey# 4 is: " + avg4);

        // Calculate least amount of food for monkey
        int minimum = monkeyarray[0][0];
        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 7; col++) {
                if (minimum > monkeyarray[row][col]) {
                    minimum = monkeyarray[row][col];
                }
            }
        }

        System.out.println("Least amount of food by any monkey: " + minimum);

        // Calculate most amount of food for monkey
        int maximum = monkeyarray[0][0];
        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 7; col++) {
                if (maximum < monkeyarray[row][col]) {
                    maximum = monkeyarray[row][col];

                }
            }
        }

        System.out.println("Most amount of food by any monkey: " + maximum);
        System.out.println("*************************************************");
        System.out.println("");
    }

}
Luminos
  • 1
  • 4
  • You aren't in the `for` loop where `col` is visible at that point. You probably should move all of that code up into the `for` loop. – Elliott Frisch Nov 19 '17 at 01:50

1 Answers1

0

first thing is 'col' is not in scope of the for loop in which you have have written the condition for validating the user to enter values from 0 to 50.

     if (val > 0 && val <= 50) {
             for (int col = 0; col < 7; col++) {
                 monkeyarray[row][col] = input.nextInt();

             }

         }

if you want col then you can have another for loop as shown in the above code

  • I have gotten the validation working thanks to some very good help. I am somewhat stumped on the average weekly consumption for each monkey. The logic seems to work, but the calculations are incorrect. I will show the code. – Luminos Dec 05 '17 at 22:24