Home > Software design >  I am trying to set my program to print out the part of my month array that correlates to the index o
I am trying to set my program to print out the part of my month array that correlates to the index o

Time:07-09

Everytime I try to use the method shown below it doesn't work out.

import java.util.Arrays;


public class FindLargests {

public static void main(String[] args) {
    //This defines the arrays
    String[] month = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
    int[] temp = {34,32,40,50,55,70,73,75,70,65,55,40}; 
    //For loop to search the temps array to find the largest value
    int max = temp[0];
    int maxIndex = -1;
        for (int i = 0; i < temp.length; i  ) {  // i --> index
                if(temp[i]>max)
                    max = temp[i];
            
                }

    
    //This prints out the result
    System.out.print("The largest value is:  "   month(i)   " - "   max);
    }
}

CodePudding user response:

Just use month[i] instead of month(i) in the final System.out.print. That should work.

CodePudding user response:

import java.util.Arrays;


public class FindLargests {

    public static void main(String[] args) {
        //This defines the arrays
        String[] month = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
        int[] temp =     {34,      32,    40,    50,    55,    70,     73,     75,    70,    65,    55,    40};
        //For loop to search the temps array to find the largest value
        int max = temp[0];
        int maxIndex = -1;

        for (int i = 0; i < temp.length; i  ) {  // i --> index

            if (temp[i] > max) {
                max = temp[i];
                maxIndex = i;//update maxIndex
            }
        }

        System.out.print("The largest value is:  "   month[maxIndex]   " - "   max);
    }
}

Output: "The largest value is: Aug - 75"

  • Related