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 9 of 9

Thread: Help with Java code,

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Java code,

    Write a program that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:

    the total rainfall for the year.
    the average monthly rainfall.
    the month with the most rain.
    the month with the least rain.

    Here's my code so far.

    import java.util.Scanner;		//Needed for Scanner class.
     
    public class Rainfall
    {
    	public static void main(String[] args)
    	{
    		double[] months = new double[12];
    		double[] rainfall = new double[12];
     
    		Scanner scn = new Scanner(System.in);
     
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			System.out.print("rainfall for month #" + (index + 1) + ": ");
    			rainfall[index] = scn.nextDouble();
    		}
     
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			TotalRainfall(rainfall[index]);
    			Average(rainfall[index]);
    			Most(rainfall[index]);
    			Least(rainfall[index]);
    		}
    	}
     
    	public static void TotalRain(double rainfall)
    	{
    		double total = 0;
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			total += rainfall[index];
    		}
    		System.out.print("Total rainfall is: " + total);
    	}
     
    	public static double Avrge(double rainfall)
    	{
    		double total = 0;
    		double average;
     
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			total += rainfall;
    		}
    		average = total / rainfall.length;
     
    		System.out.println("average monthly rainfall is: " + average);
    	}
     
    	public static double Most(double rainfall)
    	{
    		int highest = rainfall[0];
    		int month;
    		for (int index = 1; index < rainfall.length; index++)
    		{
    			if (rainfall[index] > highest)
    				{
    					highest = rainfall[index];
    					month = index;
    				}
    		}
     
    		System.out.println("Highest rain happened during month #" + month);
    	}
    	public static double Least(double rainfall)
    	{
    		int lowest = rainfall[0];
    		int month;
    		for (int index = 1; index < rainfall.length; index++)
    		{
    			if (rainfall[index] < lowest)
    			{
    				lowest = rainfall[index];
    				month = index;
    			}
    		}
     
    		System.out.println("Least rain happened during month #" + month);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java code,

    Please post any questions or problems you are having?
    If there are errors, copy the full text of the messages and post them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code,

    Quote Originally Posted by Norm View Post
    Please post any questions or problems you are having?
    If there are errors, copy the full text of the messages and post them.
    C:\Users\PSY\Desktop\Rainfall.java:38: error: double cannot be dereferenced
    for (int index = 0; index < rainfall.length; index++)
    ^
    C:\Users\PSY\Desktop\Rainfall.java:40: error: array required, but double found
    total += rainfall[index];
    ^
    C:\Users\PSY\Desktop\Rainfall.java:50: error: double cannot be dereferenced
    for (int index = 0; index < rainfall.length; index++)
    ^
    C:\Users\PSY\Desktop\Rainfall.java:54: error: double cannot be dereferenced
    average = total / rainfall.length;
    ^
    C:\Users\PSY\Desktop\Rainfall.java:61: error: array required, but double found
    int highest = rainfall[0];
    ^
    C:\Users\PSY\Desktop\Rainfall.java:63: error: double cannot be dereferenced
    for (int index = 1; index < rainfall.length; index++)
    ^
    C:\Users\PSY\Desktop\Rainfall.java:65: error: array required, but double found
    if (rainfall[index] > highest)
    ^
    C:\Users\PSY\Desktop\Rainfall.java:67: error: array required, but double found
    highest = rainfall[index];
    ^
    C:\Users\PSY\Desktop\Rainfall.java:76: error: array required, but double found
    int lowest = rainfall[0];
    ^
    C:\Users\PSY\Desktop\Rainfall.java:78: error: double cannot be dereferenced
    for (int index = 1; index < rainfall.length; index++)
    ^
    C:\Users\PSY\Desktop\Rainfall.java:80: error: array required, but double found
    if (rainfall[index] < lowest)
    ^
    C:\Users\PSY\Desktop\Rainfall.java:82: error: array required, but double found
    lowest = rainfall[index];
    ^
    12 errors

    Tool completed with exit code 1


    I'm using Textpad and these are the errors.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java code,

    The code has two definitions for the variable: rainfall. One is an array and one is a double.
    rainfall.length
    and
    rainfall[0];
    are not valid for a double. Those statements are for arrays.


    You should change one of the names so it is different. Compile the code after changing one of them and that should get rid of some of the error messages
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code,

    That really helped!

    Although, I got the program to compile correctly, I'm having a logical error.

    I can't get the program to grab which month had the most and least rain.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java code,

    which month had the most and least rain
    That will take a search through the data to find the most and least values.

    What does the program output now as a result of these searches? Post the current code to show what it is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code,

    public static void main(String[] args)
    	{
    		double[] months = new double[12];
    		double[] rainfall = new double[12];
     
     
     
    		Scanner keyboard = new Scanner(System.in);
     
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			System.out.print("Enter rainfall for month #" + (index + 1) + ": ");
    			rainfall[index] = keyboard.nextDouble();
    		}
     
    			TotalRainfall(rainfall);
     
    			Average(rainfall);
     
    			Most(rainfall, months);
     
    			Least(rainfall, months);
    	}
     
    	public static void TotalRainfall(double[] rainfall)
    	{
    		double total = 0;
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			total += rainfall[index];
    		}
    		System.out.printf("\ntotal rainfall of the year is: %.1f" , total);
    	}
     
    	public static void Average(double[] rainfall)
    	{
    		double total = 0;
    		double average;
     
    		for (int index = 0; index < rainfall.length; index++)
    		{
    			total += rainfall[index];
    		}
    		average = total / rainfall.length;
     
    		System.out.printf("\naverage monthly rainfall is: %.1f" , average);
    	}
     
    	public static void Most(double[] rainfall, double[] months)
    	{
    		double highest = rainfall[0];
    		double month;
     
    		for (int index = 1; index < rainfall.length; index++)
    		{
    			if (rainfall[index] > highest)
    				{
    					month = months[index];
    				}
    		}
    			System.out.println("\nmonth with the most rain is " + month);
    	}
    	public static void Least(double[] rainfall, double[] months)
    	{
    		double lowest = 0;
    		int month=0;
    		for (int index = 1; index < rainfall.length; index++)
    		{
    			if (rainfall[index] < lowest)
    			{
    				lowest = rainfall[index];
    				month = index;
    			}
    		}
     
    		System.out.println("month with the least rain" + month);
    	}
    }

    The output is perfectly fine.

    When it comes to the most and least rain, it always shows month as month #0

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java code,

    Try debugging the code by adding some println statements that print out the values of variables as the code executes so you can see what the computer sees as it executes the code.
    Print out the contents of the rainfall array so you can see what data it is working with. Use the Arrays class's toString() method to format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java code,

    I got it all done!

    Basically what I did was, I changed the months to String variables and I could return it just perfectly!

    Thank you Norm for you help,
    Truly appreciated!

Similar Threads

  1. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  2. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  5. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM