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

Thread: Array question. Stumped!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array question. Stumped!

    thanks for the help as always. I'm stuck on this problem and i don't know why this isn't working. This is my first time using array's so that's probably why i don't know what i am doing but here it is..

    The problem lies when calling the getHighest and getLowest method. The error i get is..

    The method getHighest(double[]) in the type RainfallClass is not applicable for the arguments ()

    Also note i'm not supposed to use any other classes just methods.

    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class RainfallClass
    {
       public static void main(String[] args)
       {
          DecimalFormat fmt = new DecimalFormat("0.00");
     
          //call the getTotal method
          double tot = getTotal();
     
          //call and pass the average method
          double average = getAverage(tot);
     
          //Call and pass the highest method
          double highest = getHighest();
     
          //Call and pass the lowest method
          double lowest = getLowest();
     
          //Display output
          System.out.println("Total\t\tAverage\t\tHighest\t\tLowest");
          System.out.println("..........................................................");
          System.out.println(fmt.format(tot) + " inches \t" + fmt.format(average) + " inches \t" 
                         + fmt.format(highest) + " inches\t" + fmt.format(lowest) + " inches");
     
       }
       public static double getTotal()
       {
     
          double total = 0.0;
          double[] inches = new double[12];
          String[] months = {"January", "February", "March", "April", "May",
                         "June", "July", "August", "September", "October",
                         "November", "December"};
     
          Scanner keyboard = new Scanner(System.in);
     
          for (int i = 0; i < inches.length; i++)
          {
             System.out.println("Enter the total rainfall for " + months);
             inches = keyboard.nextDouble();
             keyboard.nextLine();
                while (inches < 0)
                {   
                   System.out.println("Entry must be a postive number");
                   System.out.println("Enter the total rainfall for " + months);
                   inches = keyboard.nextDouble();
                   keyboard.nextLine();
                }
             total += inches;
          }
          getHighest(inches);
          getLowest(inches);
          return total;
       }
       public static double getAverage(double a)
       {
          double average = a / 12;
          return average;
     
       }
       public static double getHighest(double[] h)
       {
          double highest = h[0];
     
          for (int j = 0; j < h.length; j++)
          {
             if (h[j] > highest)
                highest = h[j];
          }
     
          return highest;
       }
       public static double getLowest(double[] l)
       {
          double lowest = l[0];
     
          for (int j = 0; j < l.length; j++)
          {
             if (l[j] < lowest)
                lowest = l[j];
          }
     
          return lowest;
     
     
       }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Array question. Stumped!

    System.out.println("Enter the total rainfall for " + months);
    It should be
    System.out.println("Enter the total rainfall for " + months[i]);
    In getHighest();
    for (int j = 0; j < h.length; j++)
    start from j=1, no need to start again from 0.
    getHighest(inches);
    getLowest(inches);
    These functions are returning double type values but you are not, catching them. MISSED!!!


    //Call and pass the highest method
          double highest = getHighest();
     
          //Call and pass the lowest method
          double lowest = getLowest();
    Why again?
    Call them at any one place. Either here or in getTotal().

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

    tjanuranus (April 5th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array question. Stumped!

    ok for some reason the code i pasted and the code i have in Eclipse is different. I do have months[i]. But the other stuff is wrong. The reason i am calling them in the getTotal method is to pass the inches to to other methods. But in the main method i want to print out a little table so i need to call them to print them out. Otherwise i'm not sure how to do it!

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

    Default Re: Array question. Stumped!

    The error you posted at the top is simple to fix. Look at your main method. You are doing the following: double highest = getHighest(); The problem here is your getHighest() method needs to take in a param of double [] because thats how you defined it. so in order to remove the error simply change your code to look something like this: double highest = getHighest(double [] i); or some array of doubles you defined earlier.

    Dejan

Similar Threads

  1. help im stumped :(
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2010, 12:51 PM
  2. Help so stumped....
    By Macgrubber in forum Loops & Control Statements
    Replies: 8
    Last Post: October 28th, 2010, 04:53 PM
  3. 2D Array Question
    By gmorris1986 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2010, 12:40 PM
  4. A question about an array
    By faizana2006 in forum Collections and Generics
    Replies: 2
    Last Post: October 28th, 2009, 05:36 PM
  5. Stumped?
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2009, 02:02 AM