I recently began working with arrays and calling method statements and it appears that somewhere within this code I have made an error. The program is supposed to allow for the input of average temperatures for each day of the week and display them. Then it is supposed to display the highest, lowest and total average of the 7 values. I am able to have it display the inputs but I cannot get the days to automatically fill in as Mon, Tues...etc. Also, the additional methods to solve for highest, lowest, and average are not appearing in the output whatsoever. Any help would be greatly appreciated. Thanks in advance.
import java.util.Scanner; public class Temperatures { public static void main(String[] args) { final int Day = 7; String[] name = {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; int[] temp = new int[Day]; Scanner readName = new Scanner(System.in); Scanner readTemp = new Scanner(System.in); System.out.println("Enter both the day and the average temperature for that day."); for (int index = 0; index < Day; index++) { System.out.print("\nDay " + (index + 1) + ": "); name[index] = readName.nextLine(); System.out.print("Average Temp " + (index + 1) + ": "); temp[index] = readTemp.nextInt(); } System.out.println("\nThe information you entered is:"); // Display the values entered. for (int index = 0; index < Day; index++) System.out.printf("%-20s %2d \n", name[index], temp[index]); } public static int findHighest(int[] A) { int highest = A[0]; int size = A.length; for (int i = 1; i < size; i++) { if (A[i] > highest) highest = A[i]; System.out.println("The highest average temp is " + highest); } return highest; } public static int findLowest(int[] A) { int lowest = A[0]; int size = A.length; for (int i = 1; i < size; i++) { if (A[i] < lowest) lowest = A[i]; System.out.println("The lowest average temp is " + lowest); } return lowest; } public static int findAverage(int[] A) { int average = A[0]; int size = A.length; average = (A[0]+A[1]+A[2]+A[3]+A[4]+A[5]+A[6])/size; System.out.println("The average of all temps is " + average); return average; } }
Output:
Day 1: Monday
Average Temp 1: 45
Day 2: Tuesday
Average Temp 2: 56
Day 3: Wednesday
Average Temp 3: 46
Day 4: Thursday
Average Temp 4: 48
Day 5: Friday
Average Temp 5: 42
Day 6: Saturday
Average Temp 6: 32
Day 7: Sunday
Average Temp 7: 37
The information you entered is:
Monday 45
Tuesday 56
Wednesday 46
Thursday 48
Friday 42
Saturday 32
Sunday 37