0

I am currently working on a problem set for an assignment at school, and I'm really close to finishing however I'm getting a logic error when running my program.

The problem set includes displaying the weeks avg. temp, highest temp., lowest temp., and the days of the week that are hottest and coldest (if there are multiple display those too).

The current problem I'm having is that my program is not displaying the days of the week if there are multiple days that are either the hottest or coldest, most likely due to my method. (weekDay).

Example what I want: sample 1:

Enter the hightest temperature of each day for a week (starting on Sunday):

11 21 15 12 21 15 9

The average temperature of the week is: 14.86 degree

The highest temperature of the week is: 21 degree

The coldest temperature of the week is: 9 degree

The hottest days of the week are: Monday, Thursday The coldest days are: Saturday

However the problem I'm having is that when I try to run my code this is what happens(copied the same temperatures from sample 1):

Sample 2:

Enter the hightest temperature of each day for a week (starting on Sunday):

11 21 15 12 21 15 9

The average temperature of the week is: 14.86 degree

The highest temperature of the week is: 21 degree

The coldest temperature of the week is: 9 degree

The hottest days of the week are: MondayMonday // This is my current issue.

The problem I believe is coming from my method called "weekDay", and most likely is that it's not calling two different days of the week, but rather only one and it gets displayed an x amount of times depending on how many temperatures are the same as max or min. It would be great if I got some guidance on this, thank you.

http://ideone.com/DAUcdR

public class test2
{
// Main method
public static void main(String[] args)
{
    // Create a new scanner
    Scanner input = new Scanner(System.in);

    // Set array list
    int[] tempList = new int[7];

    // Prompt user for input and store input
    System.out.println("Enter the hightest temperature of each day for a week (starting on Sunday): ");
        for(int i = 0; i < tempList.length; i++)
            tempList[i] = input.nextInt();

    // Averages temperature 
    double avgTemp = avgTemp(tempList);
        System.out.printf("The average temperature of the week is: %.2f degree %n", avgTemp);

    // Display hottest temperature
    int maxTemp = maxTemp(tempList);
        System.out.println("The highest temperature of the week is: " + maxTemp + " degree");

    // Display coldest temperature
    int minTemp = minTemp(tempList);
        System.out.println("The coldest temperature of the week is: " + minTemp + " degree");


    // Display hottest days of the week
    int[] maxTempList = searchTemp(tempList, maxTemp);
    System.out.print("The hottest days of the week are: ");
    for(int i = 0; i < maxTempList.length; i++)
        System.out.print(weekDay(maxTemp,tempList));

    // Display the coldest days of the week
    int[] minTempList = searchTemp(tempList, minTemp);
    System.out.println("\n The coldest days of the week are: ");
    for(int i = 0; i < minTempList.length; i++)
        System.out.print(weekDay(minTemp,tempList));
}

// Average the temperature
public static double avgTemp(int[] array)
{
    // Set a total temperature variable
    int tempTotal = array[0];

    // Add all temperature values
    for(int i = 1; i < array.length; i++)
        tempTotal = array[i]+tempTotal;

    // Return temperature average.
    return ((double)tempTotal/array.length);
}

// Get hottest temperature
public static int maxTemp(int[] array)
{
    // Set hottest day variable
    int max = array[0];

    // Check and replace max temperature
    for(int i = 1; i < array.length; i++){
        if(max < array[i])
            max = array[i];

    }
    return max;
}

// Get coldest temperature
public static int minTemp(int[] array)
{
    // Set coldest day variable
    int min = array[0];

    // Check and replace coldtest temperature
    for(int i = 1; i < array.length; i++){
        if(min > array[i])
            min = array[i];
    }
    return min;
}

public static String weekDay(int i, int[] array)
{
    int[] displayWeekDay = searchTemp(array, i);
    String[] weekDay = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    for(int j = 0; j < displayWeekDay.length; j++){
        int days = displayWeekDay[j];
        return weekDay[days];
    }
    return null;
}
// Finds the index of the hottest/coldest days
public static int[] searchTemp(int[] temp, int key)
{

    int count = 0;
    // Searches the array for the index where the element value is the same
    for(int i = 0; i < temp.length; i++){
        // When the number is the same as the key, increase count
        if(temp[i] == key)
            count++;
    }

    // Create index array based on same number to the key
    int[] index = new int[count];
    // Copy index numbers of the key into index array
    for(int j = 0; j < index.length; j++){
        for(int i = 0; i < temp.length; i++){
            if(temp[i] == key){
                if(j > 0 && index[j - 1] == i)
                    continue;
                else{
                    index[j] = i;
                    break;
                }
            }
        }
    }
    return index;
}

}

0

2 Answers 2

1

Modify your weekDay method like below it will return an array of weekdays with max temprature then you can use it to print those days:

public class DisplayWeekTempStat {
// Main method
public static void main(String[] args) {
    // Create a new scanner
    Scanner input = new Scanner(System.in);

    // Set array list
    int[] tempList = new int[7];

    // Prompt user for input and store input
    System.out.println("Enter the hightest temperature of each day for a week (starting on Sunday): ");
    for (int i = 0; i < tempList.length; i++)
        tempList[i] = input.nextInt();

    // Averages temperature
    double avgTemp = avgTemp(tempList);
    System.out.printf("The average temperature of the week is: %.2f degree %n", avgTemp);

    // Display hottest temperature
    int maxTemp = maxTemp(tempList);
    System.out.println("The highest temperature of the week is: " + maxTemp + " degree");

    // Display coldest temperature
    int minTemp = minTemp(tempList);
    System.out.println("The coldest temperature of the week is: " + minTemp + " degree");

    // Display hottest days of the week

    System.out.print("The hottest days of the week are: ");
    String[] tempMaxWeekDay = weekDay(maxTemp, tempList);
    for (int num = 0; num < tempMaxWeekDay.length; num++) {
        if (tempMaxWeekDay[num] != null)
            System.out.println(tempMaxWeekDay[num]);
    }

    // Display the coldest days of the week
    System.out.println("\n The coldest days of the week are: ");
    String[] tempMinWeekDay = weekDay(minTemp, tempList);
    for (int num = 0; num < tempMinWeekDay.length; num++) {
        if (tempMinWeekDay[num] != null)
            System.out.println(tempMinWeekDay[num]);
    }
}

// Average the temperature
public static double avgTemp(int[] array) {
    // Set a total temperature variable
    int tempTotal = array[0];

    // Add all temperature values
    for (int i = 1; i < array.length; i++)
        tempTotal = array[i] + tempTotal;

    // Return temperature average.
    return ((double) tempTotal / array.length);
}

// Get hottest temperature
public static int maxTemp(int[] array) {
    // Set hottest day variable
    int max = array[0];

    // Check and replace max temperature
    for (int i = 1; i < array.length; i++) {
        if (max < array[i])
            max = array[i];

    }
    return max;
}

// Get coldest temperature
public static int minTemp(int[] array) {
    // Set coldest day variable
    int min = array[0];

    // Check and replace coldtest temperature
    for (int i = 1; i < array.length; i++) {
        if (min > array[i])
            min = array[i];
    }
    return min;
}

public static String[] weekDay(int i, int[] array) {
    String[] maxWeekDays = new String[7];
    String[] weekDay = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int k = 0;
    for (int j = 0; j < weekDay.length; j++) {
        if (array[j] == i) {
            maxWeekDays[k] = weekDay[j];
            k++;
        }
    }
    return maxWeekDays;
}

}

Sign up to request clarification or add additional context in comments.

11 Comments

you have to replace this one too in out code to print the days returned in array from this weekDay method : // Display hottest days of the week int[] maxTempList = searchTemp(tempList, maxTemp); System.out.print("The hottest days of the week are: "); for (String s : weekDay(maxTemp,tempList)) { if (s != null) System.out.println(s); }
I have edited whole code for you pls try now it will work :)
Just copy paste whole code from my solution above it will work
you are not changing the return type of the weekDay method i have modified complete code in my answer pls use that.
you wanna use normal for loop if yes, yes normal for loop can also be used but in case of Arrays and collections this enhanced loop works much better
|
1

Your weekDay() method is basically doing the same thing as searchTemp() method. If you look into weekDay() you will see internally you are calling searchTemp(). Remove searchTemp() from weekDay() method, may be do something like this:

public static String weekDay(int i)
{   int currentDay = 0;
    if((i / 7) == 0){
        currentDay = i;
    }
    else{
        currentDay = i % 7;
    }
    String[] weekDay = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    return weekDay[currentDay];
}

This assumes that you don't care about what month it is, and the month always starts on a Sunday

6 Comments

I get this error when I run my code: The hottest days of the week are: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15 at javaTest.weekDay(javaTest.java:100) at javaTest.main(javaTest.java:45)
You weekDay() function needs a little bit of work, try and see what I've done on my edited answer
I'm not sure why you're trying to do with currentDay = i%7;?
You pass the day of the month to weekDay() method, what i % 7, does is it returns the day of week. Which you then plug into weekDay array to get the name of the day of week.
Do you mean I pass the day of the week to weekDay() method? It doesn't make sense since the index of the hottest days are already given, it gives me a random day of the week, say the highest temperature is on Monday (element 1), then if it goes into your "int currentDay = i%7;" it gives 6 as the remainder which displays Saturday...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.