morgh morgh - 1 year ago 83
Java Question

Display String and Int Array at once

I am not sure how to display a string and int array at the same time so that it will display the maximum value of the array with the specific month

static double[] set2014 = new double[6];
static String[] months = new String[6];

Here is the method for calculating the maximum value:

public static void max(){
double max = set2014[0];

for(int i = 1; i < set2014.length; i++){
if(set2014[i] > max){
max = set2014[i];
System.out.println("Largest figure is " + max);

For example, the output will be:
Largest figure is:
March 204566

Answer Source

Maintaining two arrays that are linked by index is what I call Object Denial. Consider creating a class that contains both month and value.

public interface MonthValue { //class or interface, I just didn't want to type out the simple implementation
   String getMonth();
   double getDouble();

//set2014 now needs to contain MonthValues
MonthValue max = set2014[0];

for(int i = 1; i < set2014.length; i++){
    MonthValue current = set2014[i];
    if(current.getValue() > max.getValue()){
        max = current;

System.out.println("Largest figure is " + max.getValue());
System.out.println("In month " + max.getMonth());

But to answer your question as is:

You can keep track of the index instead:

int maxMonthIndex = 0;

for(int i = 1; i < set2014.length; i++){
    if(set2014[i] > set2014[maxMonthIndex]){
        maxMonthIndex = i;

System.out.println("Largest figure is " + set2014[maxMonthIndex]);
System.out.println("In month " + months[maxMonthIndex]);

By the way, set is a bad name for an array. Sets are unordered collections, arrays have an order.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download