Hi everyone, I just started on learning java and I have this problem I'm trying to figure out.
The question is asking me to have my program read the twelve temperature values and print the month with the highest temperature.

This is what I have so far:

public static void main(string[] args)
{
    double highestValue;
    highestValue = in.nextDouble();
    int highestMonth=1;
    for (int currentMonth = 2; currentMonth <= 12; currentMonth++)
    {
	double nextValue = in.nextDouble();
        if (nextValue > highestValue)
        {
	   highestValue = nextValue;
           highestMonth = currentMonth;
	}
}
System.out.println(highestMonth);

Okay? And your question is? (Assuming "in" is a static import of System.in)

The code is from the book i'm reading. I'm very new to this and just confuse by it.
But the program i'm trying to make should read the twelve temperature values and print the highest one. I just don't know where and how should i implement that into the above code.

That code is reading twelve values and printing the (number of the) month corresponding to the highest value. What else do you want? Maybe you should see the comment about the static import. If you want the highest temp and not the month containing it, then simply print the other value.

what i'm confuse about is where would/should i input the 12 temp. values so the program can read those and pick and print the highest value. The program doesn't just randomly generate 12 values and pick the highest, right?

Each place where you see in.nextDouble();, that is where your program is trying to read input from the user. In general it is bad form to have an input statement without an output statement to prompt the user first. So just before lines 4 and 8 I would insert a line something like this:

System.out.print("Enter a temperature: ");

Then when you run the program it is more clear that the program is waiting for the user to do something (enter a number). At the prompt, you can enter a number using the keyboard, and then press the enter key.

Finally, it is also bad form to simply output a number like the last line of your program. It is much better to say what that number means. Something like:

System.out.println("The month with the highest temperature is " + highestMonth);

This is what i have so far:

import java.util.*;
public class Temperature
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner(System.in);
        
        System.out.print("What's the temperature in January?");
        double January = in.nextDouble();
        
        System.out.print("What's the temperature in February?");
        double February = in.nextDouble();
        
        double highestValue;
        highestValue = in.nextDouble();
        int highestMonth = 1;
        for (int currentMonth = 2; currentMonth <= 12; currentMonth++)
        {
            double nextValue = in.nextDouble();
            if (nextValue > highestValue)
            {
                highestValue = nextValue;
                highestMonth = currentMonth;
            }
        }
        System.out.println("The month with the highest temperature is " + highestMonth);
    }
}

Should I continue to write each month and prompt the user for 12 inputs or?

If you have to have the names of the months, I can show you a way to do that. But to make things simpler, you could start with a statement like this:

System.out.println("Enter a temperature for each of the 12 months.");

Then inside your loop you can do this:

System.out.print("Temperature for month " + currentMonth + ": ");
double nextValue = in.nextDouble();

In this case, your only input statement should be inside the loop, and your current month should start at 1 instead of 2.

New code:

import java.util.*;
public class Temperature
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner(System.in);
        
        double highestValue;
        highestValue = in.nextDouble();
        int highestMonth = 1;
        for (int currentMonth = 2; currentMonth <= 12; currentMonth++)
        {
            System.out.print("Temperature for month " + currentMonth + ": ");
            double nextValue = in.nextDouble();
            if (nextValue > highestValue)
            {
                highestValue = nextValue;
                highestMonth = currentMonth;
            }
        }
        System.out.println("The month with the highest temperature is " + highestMonth);
    }
}

I did that and the virtual machine wouldn't run0_o It takes a very long time for it to load.

You still have one input statement (on line 9) outside the loop, with no corresponding output. It looks like the program is taking a very long time, when it is really just waiting for you to give it some input. Take line 9 out of your program. Instead, initialize highestValue to something low like 0 or -1.

yay! it logically works now. Thanks a lot! But my problem is how do i convert the "month" name to the actual name of the month?

Temperature for month 2: 45
Temperature for month 3: 78
Temperature for month 4: 48
Temperature for month 5: 78
Temperature for month 6: 4
Temperature for month 7: 5
Temperature for month 8: 87
Temperature for month 9: 12
Temperature for month 10: 100
Temperature for month 11: 65
Temperature for month 12: 46
The month with the highest temperature is 10

The teacher would probably accept the output: The month with the highest temperature is month 10, which is October. But I just want to know/learn how to do the conversion.

Note that your loop is starting at 2, so you are only getting 11 months. Change the initial setting in your loop to start at 1 instead.

If you want to print the actual names of the months, you can create an array of Strings like this:

String monthNames[] = {"January", "February", ... "December"};  // fill in all the names

Then when you want to print one of these names you need to index into the array. Note that array indices start at 0 in Java. So change your output line to something like this:

System.out.print("Temperature for " + monthNames[currentMonth] + ": ");

Now you need your loop to start at 0 instead of 1 or 2, and the <= needs to change to <, like this:

for (int currentMonth = 0; currentMonth < 12; currentMonth++)

Output:
Enter a temperature for each month:
Temperature for January: 11.1
Temperature for February: 11.2
Temperature for March: 11.3
Temperature for April: 11.4
Temperature for May: 11.5
Temperature for June: 11.6
Temperature for July: 11.7
Temperature for August: 11.8
Temperature for September: 11.9
Temperature for October: 12.0
Temperature for November: 12.1
Temperature for December: 12.2
The month with the highest temperature is December

So much better. I think that is the whole program:D

Thank you very much kramerd. I greatly appreciate your effort, time, and help.
I wish I could pay you or something lol

Note that your loop is starting at 2, so you are only getting 11 months. Change the initial setting in your loop to start at 1 instead.

That's because the code was written to ask for, and read, the first value before entering the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.