1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

[Java] What am I doing wrong?

Discussion in 'BBS Hangout' started by gwatson86, Apr 27, 2010.

Tags:
  1. gwatson86

    gwatson86 Contributing Member

    Joined:
    Jan 4, 2003
    Messages:
    2,664
    Likes Received:
    191
    I'm in an introductory Computer Science class learning Java, and I can't figure out where my problem lies here. When I try to compile, I get a slew of errors that seem to stem from the "leastRainfall" method. Any of you guys well-versed in Java care to see if you can't figure out my problem(s)? Useful info = rep! :cool:

    P.S. Sorry the code is kinda sloppy, my copy/paste job didn't carry over my indentations and such very well.


    import java.util.Scanner;

    public class Rainfall

    {
    public static void main(String[] args)
    {
    final double months = 12;
    double[] rainfall = new double[months];

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the amount of rainfall in inches for each of the " + months + " months.");

    for (double x = 0; x < months; x++)
    {
    System.out.print("Month " + (index + 1) + ": ");
    months[index] = keyboard.nextDouble();

    if (rainfall < 0)
    {
    System.out.println("Inches of rain cannot be less than zero.");
    System.exit(0);
    }
    }

    totalRainfall(rainfall);
    averageRainfall(rainfall);
    mostRainfall(rainfall);
    leastRainfall(rainfall);

    }

    public static void totalRainfall(int[] rainfall)
    {
    double total = 0;

    for (double a = 0; a < rainfall.length; a++)
    total += rainfall[a];

    System.out.println("The total amount of rainfall is " + total + " inches.");
    }

    public static void averageRainfall(int[] rainfall)
    {
    double total = 0;
    double average = 0;

    for (double b = 0; b < rainfall.length; b++)
    total += rainfall;
    average += total / rainfall.length;

    System.out.println("The average monthly rainfall is " + average + " inches.");
    }

    public static void mostRainfall(int[] rainfall)
    {
    double highest = rainfall[0];

    for (double c = 1; c < rainfall.length; c++)
    {
    if (rainfall[c] > highest)
    highest = rainfall[c];

    System.out.println("The highest amount of rainfall is " + highest + " inches.");
    }

    public static void leastRainfall(int[] rainfall)
    {
    double lowest = rainfall[0];

    for (double d = 1; d < rainfall.length; d++)
    {
    if (rainfall[d] < highest)
    lowest = rainfall[d];

    System.out.println("The lowest amount of rainfall is " + lowest + " inches.");
    }
    }
    }
     
  2. moestavern19

    moestavern19 Member

    Joined:
    Dec 8, 1999
    Messages:
    39,003
    Likes Received:
    3,638
    What the FUFK?
     
  3. mazyar

    mazyar Member

    Joined:
    Jun 10, 2008
    Messages:
    1,447
    Likes Received:
    17
  4. gwatson86

    gwatson86 Contributing Member

    Joined:
    Jan 4, 2003
    Messages:
    2,664
    Likes Received:
    191
    [​IMG]
     
  5. Ron from the G

    Ron from the G Contributing Member

    Joined:
    Feb 14, 2008
    Messages:
    1,091
    Likes Received:
    77
    Your using highest in leastRainfall when it should be lowest. Highest is not declared in leastRainfall. You should probably be using lowest instead where your code is highlighted in red.

    public static void mostRainfall(int[] rainfall)
    {
    double highest = rainfall[0];

    for (double c = 1; c < rainfall.length; c++)
    {
    if (rainfall[c] > highest)
    highest = rainfall[c];

    System.out.println("The highest amount of rainfall is " + highest + " inches.");
    }

    public static void leastRainfall(int[] rainfall)
    {
    double lowest = rainfall[0];

    for (double d = 1; d < rainfall.length; d++)
    {
    if (rainfall[d] < highest)
    lowest = rainfall[d];
     
    1 person likes this.
  6. moestavern19

    moestavern19 Member

    Joined:
    Dec 8, 1999
    Messages:
    39,003
    Likes Received:
    3,638

    [​IMG]
     
  7. Vengeance

    Vengeance Contributing Member

    Joined:
    Nov 29, 2000
    Messages:
    5,894
    Likes Received:
    20
    It's been over five years since I taught Java, and a few years since I last did any programming, but as far as I can tell, the variable "highest" is not declared in your leastrainfall method. Also, it doesn't look to me like lowestrainfall does what you want it to do. It looks like you're iterating through the array and checking against the highest rainfall, and if the current item in the array is lower, you are calling it the lowest rainfall. Also, I think it also is printing out the lowest rainfall during each iteration of the loop. Although you may want it to do that so that may be intentional.
     
    #7 Vengeance, Apr 27, 2010
    Last edited: Apr 27, 2010
    1 person likes this.
  8. ClutchCity3

    ClutchCity3 Member

    Joined:
    Mar 17, 2008
    Messages:
    2,752
    Likes Received:
    376
    dude you missed a bracket..

    other than that....there were 16 errors found according to Dr.Java...

    you might want to fix it by yourself first...



     
    1 person likes this.
  9. gwatson86

    gwatson86 Contributing Member

    Joined:
    Jan 4, 2003
    Messages:
    2,664
    Likes Received:
    191
    Thanks to all three of you; my tired eyes are overlooking things that ought to be easy. CMD was showing 6 errors before I fixed my lowest variable and added that bracket in... now I've got 15 more to figure out. Much obliged, guys.
     
  10. Air Langhi

    Air Langhi Contributing Member

    Joined:
    Aug 26, 2000
    Messages:
    21,633
    Likes Received:
    6,263
    Why are you making all your methods static?
     
  11. gwatson86

    gwatson86 Contributing Member

    Joined:
    Jan 4, 2003
    Messages:
    2,664
    Likes Received:
    191
    Probably because I don't know enough about this stuff to know why I shouldn't.
     
  12. gwatson86

    gwatson86 Contributing Member

    Joined:
    Jan 4, 2003
    Messages:
    2,664
    Likes Received:
    191
    Successfully compiled and executed, thanks all for your help. :)
     
  13. Air Langhi

    Air Langhi Contributing Member

    Joined:
    Aug 26, 2000
    Messages:
    21,633
    Likes Received:
    6,263
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication2;
    import java.util.Scanner;
    
    // <editor-fold defaultstate="collapsed" desc="comment">
    public class Rainfall// </editor-fold>
    {
        public static void main(String[] args)
        {
            final int months = 12;
            double[] rainfall = new double[months];
    
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Enter the amount of rainfall in inches for each of the " + months + " months.");
    
            for (int x = 0; x < months; x++)
            {
                System.out.print("Month " + (x + 1) + ": ");
                rainfall[x] = keyboard.nextDouble();
    
                if (rainfall[x] < 0.0)
                {
                    System.out.println("Inches of rain cannot be less than zero.");
                    System.exit(0);
                }
            }
    
            Rainfall.totalRainfall(rainfall);
            Rainfall.averageRainfall(rainfall);
            Rainfall.mostRainfall(rainfall);
            Rainfall.leastRainfall(rainfall);
    
        }
    
        public static void totalRainfall(double[] rainfall)
        {
            double total = 0;
    
            for (int a = 0; a < rainfall.length; a++)
            total += rainfall[a];
    
            System.out.println("The total amount of rainfall is " + total + " inches.");
        }
    
        public static void averageRainfall(double[] rainfall)
        {
            double total = 0;
            double average = 0;
    
            for (int b = 0; b < rainfall.length; b++)
            total += rainfall[b];
            average += total / rainfall.length;
    
            System.out.println("The average monthly rainfall is " + average + " inches.");
        }
    
        public static void mostRainfall(double[] rainfall)
        {
            double highest = rainfall[0];
    
            for (int c = 1; c < rainfall.length; c++)
            {
                if (rainfall[c] > highest)
                    highest = rainfall[c];
    
                System.out.println("The highest amount of rainfall is " + highest + " inches.");
            }
        }
    
        public static void leastRainfall(double[] rainfall)
        {
        double lowest = rainfall[0];
    
        for (int d = 1; d < rainfall.length; d++)
            {
                if (rainfall[d] < lowest)
                    lowest = rainfall[d];
    
                System.out.println("The lowest amount of rainfall is " + lowest + " inches.");
            }
        }
    }
    
    I no idea if the logic is right, but at least the code should compile. Don't use a double to iterate through an array.
     
    1 person likes this.
  14. gwatson86

    gwatson86 Contributing Member

    Joined:
    Jan 4, 2003
    Messages:
    2,664
    Likes Received:
    191
    Very nice thought to actually go through and detoxify that mess I posted a couple hours ago, but I've got it all squared away now. I did figure out not to use a double, but why is that exactly?

    Also, you never clarified why I should or should not make a method static.
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now