ssethzz
ssethzz

Reputation: 43

Getting specific user input in 2D arrays

Here's My Assignment:

"Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job.

You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can be the rows and the departments can be the columns. Have the program ask the user three times for a bonus amount, grocery store number (1-4), and department number (1-3) and add the bonus amount to the appropriate place in the array based on the numbers they gave. After this is done, add up the bonuses for each of the 4 stores [the 4 rows in your 2 dimensional array] and display them for the user."

Here's My Code:

import java.util.Scanner;
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);

        double table[][] = new double[4][3];

        System.out.printf("Please enter a bonus amount for three%n" +
                "stores out of four stores%nand each of their three " +
                "departmanets.%n%nThese departments include:%n" +
                "Produce%nMeat%nFrozen%n%n");

        for (int i = 1; i <= 3; i++) {
            System.out.printf("Please enter the store number (1-4)%n");
            int a = in.nextInt();
            while (a > 4 || a < 1) {
                System.out.printf("Please enter a number 1-4%n");
                a = in.nextInt();
            }
            System.out.printf("%nPlease enter the department number:%n");
            int b = in.nextInt();
            while (b > 3 || b < 1) {
                System.out.printf("Please enter a number 1-3%n");
                b = in.nextInt();
            }

            int c = a - 1;
            int d = b - 1;
            System.out.printf("%nPlease enter bonus amount:%n");
            double price = in.nextDouble();
            table[c][d] = price;
        }

        for (int j = 0; j <= table.length; j++) {
            for (int k = 0; k <= table[j].length; k++) {
                System.out.printf("%d ", table[j][k]);
            }
        }
    }
}

My Question:

How do I get a user's input (the bonus amount) to a specific spot in the 2-D array by just prompting the user to enter the store number and department number. And then, how do I print it after?

Upvotes: 0

Views: 721

Answers (1)

Zach Rieck
Zach Rieck

Reputation: 451

I think your question isn't asking for 3 inputs. It's asking for 3 inputs on each field. So I believe you're looking to populate a 4x3 array by asking the user to provide the details for the bonus, store, and department 3 times. The 4th store is then filled in by default.

Personally, I would suggest having the user input the bonus 1st and then create the 2d array. Also, if you want to get fancy (maybe you don't care haha) you can change your while statement to catch any bad entry. Right now, if someone enters a string, float, etc, it will return an exception. This is thankfully fairly easy to fix though

Scanner in = new Scanner(System.in);
//Better to cache dimensions in an array so it's more easily modifiable
int stores = 4;
int depts = 3;
double table[][] = new double[stores][depts];

//Create an array to hold user inputs
int inputs[][] = new int[stores][depts];

//Use a for loop to populate table
for (int i = 1, i <= stores; i++) {
    System.out.println("Enter the bonus here.");
    double bonus = in.nextDouble;
    //While loop to catch mischievous input
    while (!bonus.hasNextDouble) || bonus <= 0) {
        System.out.println("Invalid input :/");
        double bonus = in.nextDouble();
    }

    System.out.println("Enter store number here.");
    int a = in.nextInt();
    while (!a.hasNextInt || a > 4 || a < 1) {
        System.out.println("Stop trying to break my code!");
        int a = in.nextInt();
    }

    System.out.println("Enter dept number here.")
    int b = in.nextInt();
    //Same while loop with b :)

    table[a - 1][b - 1] = bonus;
    inputs[i - 1][i - 1] = {{a}, {b}}
    //You could catch this with a while loop too using if (!inputs[a][b] = 0)  }

Executing the above fills the array at the exact requested position with the specified bonus value for 3 stores. I'm not exactly sure what's intended for the 4th store given that no pattern is specified where you could calculate the value. If it were me, I would seek clarification on that, but alternatively we can assume that the other values fill with 0's. If that's the case, the array is now all set since arrays initialize to a default value of 0. So now, you just need to sum up the rows and print it out.

for (int i = 1; i <= stores; i++) {
    int sum = 0;
    for (int j = 1; j <= stores; j++) {
        sum += table[i][j];
    }
    System.out.println("Total store bonus for store " + i + ": " + sum);
}

You could technically put that in the above loop and do everything at once since it uses the same parameters, but I find that a bit clunky and hard to read personally. Hope that helps!

Upvotes: 1

Related Questions