Stuck on errors (Java)

Please assist,

Not sure where the error is coming from or what I’m missing, this is from the following question

Write the complete java program called Temperature that includes a for loop structure, prompting the user to enter 5 temperature values. The program should add up each temperature entered within the loop and this is stored in a variable called total. Using a system statement, output the total temperature value and the average temperature value. Use the sample output below as a guide:
The total temperature =
The average temperature =

<p>import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner temp = new Scanner (System.in);
        double total = 0;
        for(int=1;int<=5;int++);
        {
            System.out.println ("Enter temperature #" + temp +":");
            double temperature = temp.nextDouble ();
            total = temperature;

        }
        System.out.println("The total temperature =" +total);
        System.out.println("The average temperature=" +(double)(total/5));
    }
}
<p/>

You missed few things as follows -

for loop missing i initialization,
also extra semicolon at end of for

“Enter temperature #” + i + “:”

total = total + temperature;

Hi,

Thanks for the feedback.

So I shouldn’t use temp?

I changed it from “i” as I was getting an error and I’m still getting the error

Scanner object (in your case temp) only used while reading input
it should not be used in System.out.println

use i instead so that it will show, i’s value 1 2 3 4 5
Enter temperature #1
Enter temperature #2
Enter temperature #3
Enter temperature #4
Enter temperature #5

I always declare like- Scanner sc= new Scanner (System.in);
then use it as- sc.nextDouble()

1 Like
import java.util.Scanner;

public class Temperature {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double total = 0.0;
        double temperature = 0.0;

        for (int t = 1; t <= 5; t++) {
            System.out.print("Enter temperature #" + t + ": ");
            temperature = scanner.nextDouble();
            total = total + temperature;
        }

        System.out.println("The total temperature = " + total);
        System.out.println("The average temperature = " + (total / 5));
    }
}

Thanks, EricNC,

I managed to figure out why the error in my code

jGrasp needed me to do the following

<p>
import java.util.Scanner;
</p>
 
And with the above I had  

import javax.swing.JOptionPane;

1 Like