Home > database >  How do I list the array number which has the highest value?
How do I list the array number which has the highest value?

Time:11-26

In a current assignment I am trying to print out the array which has the highest value along with the array number itself. However with the code I have it is printing the array number as the same as the the array value. With the input values of 4, 9, 3, 7, and 6 the output should be Day #2 with 9 hours worked, but it outputs Day #9 with 9 hours worked. Any help appreciated!

import java.util.*;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        Scanner userinput = new Scanner(System.in);
        System.out.print("Enter hours worked on day #1: ");
        int Day1hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #2: ");
        int Day2hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #3: ");
        int Day3hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #4: ");
        int Day4hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #5: ");
        int Day5hours = userinput.nextInt();

        ArrayList<Integer> day = new ArrayList<Integer>();
        day.add(Day1hours);
        day.add(Day2hours);
        day.add(Day3hours);
        day.add(Day4hours);
        day.add(Day5hours);
        double avg;
        int sum = 0;
        int mosthours = Collections.max(day);

        System.out.println("The most hours worked was on: ");
        for (int i = 0; i < day.size(); i  ) {
            int daywithmosthours = day.get(i);
            if (day.get(i) == mosthours) {
                System.out.println("Day #"   daywithmosthours   " when you worked "   mosthours   " Hours.");
            }
        }

    }
}


CodePudding user response:

The problem is in your for-loop. Change it into

for (int i = 0; i < day.size(); i  ) {
    int daywithmosthours = i;
    if (day.get(i) == mosthours) {
        System.out.println("Day #"   daywithmosthours   " when you worked "   mosthours   " Hours.");
    }
}

CodePudding user response:

As mentioned in the other answer

int daywithmosthours = day.get(i);

should be

int daywithmosthours = i;

This will get you the day index rather than the value at the index. How ever index start with 0 and it will be one less than the expected value. You should add 1 to fix this. For a beginners standpoint this program will work , but this can be improved further as mentioned in the above comments.

CodePudding user response:

I feel like your use of Collections.max(day) is cheating.

Here's probably how most people would solve this assignment:

  public static void main( String [] args) {   
    Scanner userinput = new Scanner(System.in);
    ArrayList<Integer> day = new ArrayList<Integer>();
    for(int i=1; i<=5; i  ) {
      System.out.print("Enter hours worked on day #"   i   ": ");
      day.add(userinput.nextInt());  
    }

    int index = 0;
    int max = day.get(index);
    for (int i=1; i<day.size(); i  ) {
      int cur = day.get(i);
      if (cur > max) {
        max = cur;
        index = i;
      }
    }
    
    System.out.println("The most hours worked was on: ");
    System.out.println("\tDay #"   (index 1)   " when you worked "   max   " Hours.");
  }
  • Related