Rachel
Rachel

Reputation: 57

How can I display the largest amount in my 2D array?

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Random;

public class labb8 {
  public static void main(String[] args) {
    Random rnd = new Random();
    int[][] sales = new int[5][7];
    int[] total = new int[sales.length];
    ArrayList<String> unpopularSoftware = new ArrayList<String>();

    String[] locations = {"Houston", "Dallas", "Hunts", "San Ant", "Austin"};

    System.out.println("Location\t| Software Sales\t\t| Total Sales");
    System.out.println("--------------------------------------------------");

    for (int i = 0; i < sales.length; i++) {
      for (int j = 0; j < sales[i].length; j++) {
        sales[i][j] = rnd.nextInt(25);
        total[i] += sales[i][j];
      }
      System.out.print(locations[i] + "\t\t|");
      System.out.print(" " + Arrays.toString(sales[i]));
      System.out.println("\t| " + total[i]);
    }

    int unpopularModelCounter;
    for (int j = 0; j < sales[0].length; j++) {
      unpopularModelCounter = 0;
      for (int i = 0; i < sales.length; i++) {
        if (sales[i][j] != 0) {
          unpopularModelCounter++;
        }
      }
      if (unpopularModelCounter <= 3) {
        unpopularSoftware.add("Software " + (j + 1));
      }
    }
    System.out.println("Unpopular Software: " + unpopularSoftware);
    System.out.println("Location with most sold licenses: ");
  }
}

Above is the code and it gives me the results I'm looking for; however, I'd like to know some methods on how can I print out the name of the location that has the most sold software? What I've tried is putting System.out.println(total[i]); under System.out.println("\t| " + total[i]); but that just displayed all the totals, which is what I figured would happen. I've also tried putting System.out.println(total[i]); where the other output lines are at the bottom(which is where I want it), but the code can't find [i], which made me believe that I might have to create some methods; so, again, I'm asking for some course of advice on how to print out the name of the city with the largest amount sold in terms of my code.

Upvotes: 1

Views: 50

Answers (2)

Kevin Anderson
Kevin Anderson

Reputation: 4592

If you want the name of the city the the highest total, you need to look through the total array for the largest total and, while doing that, keep track of both the largest total you've seen so far and the index at which you saw that largest total.

Something like this should do the job nicely:

// Our initial guess is that `total[0]` is the maximum sales total.
int maxSales = total[0];
int maxI = 0;
for (int i = 1; i < total.length; ++i) {
    if (totals[i] > maxSales) {
        maxSales = total[i];
        maxI = i;
    }
}
System.out.println(locations[maxI] + " has the most sales: " + maxSales);

Upvotes: 2

TheXDShrimp
TheXDShrimp

Reputation: 116

Maybe try adding this to the end of the code.

int max = 0; // stores the maximum value
for (int i = 0; i < total.length; i++) {
    max = Math.max(max, total[i]);
}
System.out.println(max);

This should print the greatest value of the total array. I like your code!

Upvotes: 1

Related Questions