Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: [Method] needing some help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default [Method] needing some help

    First I want to start explaining what the programs purpose. I am asking the user to input temperatures for an 8 hour span of time. So they input 8 numbers the program then prints those numbers on the screen like so:
    Enter temperature for hour 1: .....
    Enter temperature for hour 2: .....

    Temperature Report
    The temperature of hour 1: 68
    The temperature of hour 2: 78
    etc.

    now this is where im running into trouble. I'm trying to get the average of all the temperatures that are in the array which I have the formula for and I think that its correct but whats happening I think has to do with how my for loop is stated and its printing an average number for each single temperature instead of giving me just the average of all the temperatures together. Below is my code and the average part is at the bottom of the code. Any help would be appreciated.
    import java.util.*;
    public class temperature
    {
        static Scanner console = new Scanner(System.in);
        public static void main(String[] args)
        {
                    int[] temp;
                    temp = new int[8];
    		double average;
    		int count;
    		int sum;
     
     
    		for(count = 0; count <temp.length;count++)
    		{
    			System.out.print("Enter temperature for hour " + (count + 1) + " : ");
    			temp[count] = console.nextInt();
    		}
     
    		System.out.println();
    		System.out.println();
    		System.out.println("Temperature Report");
     
    		for(count = 0; count <temp.length; count++)
    		{
    			System.out.println("Hour " + (count + 1) + " : " + temp[count]);
    		}
    		sum = 0;
    		for(count = 0; count < temp.length; count++)
    		{
    			sum = sum + temp[count];
    			average = sum / temp.length;
     
    			System.out.println("Average Temperature: " + average);
    	         }
    	}
    }
    Last edited by Perplexing; December 2nd, 2010 at 05:01 PM. Reason: came across same new problems.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: [Method] needing some help

    You need to specify an index.

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Perplexing (November 30th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 6 Times in 3 Posts

    Default Re: [Method] needing some help

    Yea, you need to use your index, which in your case is counter.

    sum += a[counter];


    Dejan

  5. The Following User Says Thank You to Dejan For This Useful Post:

    Perplexing (November 30th, 2010)

  6. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: [Method] needing some help

    thank you so much for the help. it is greatly appreciated.

  7. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: [Method] needing some help

    problem has been fixed thanks for any previous help I was given.
    Last edited by Perplexing; December 3rd, 2010 at 08:18 AM. Reason: solved

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM