Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Can someone take a quick look at this array/forloop. java program for me?

    This program is suppose to take gas prices and find the average using arrays and stuff.
    This one keeps telling me the average is 00.0 which it isnt.

    I'm pretty sure I named something wrong or something, but Im not sure what.

    import java.util.*;

    public class AverageGasPrice
    {

    public static void displayHeader()
    {
    System.out.println("K");
    System.out.println("ID ");
    System.out.println("This program takes sevral gas prices and outputs their average");
    System.out.println("November 23 2010");
    }


    public static int readPrices(double[] gasPrice)
    {
    int priceCount = 0;
    double price = 0;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("This program calculates and displays the average");
    System.out.println("price of a regular gallon of gas.");

    System.out.println("Enter the price of gas or -99 to Quit: ");
    price = keyboard.nextDouble();

    while(price != -99)
    {
    gasPrice[priceCount] = price;
    priceCount++;
    price = keyboard.nextDouble();

    }

    return priceCount;

    }

    public static double averagePrice(double[] gasPrice, int priceCount)
    {

    double sum = 0;

    //Compute the sum
    for(int i=0; i < priceCount; i++)
    {
    sum = (gasPrice[priceCount] + sum);

    }
    //Compute the average
    double average = sum/priceCount;

    return average;
    }

    public static void main(String[] args)
    {
    displayHeader();
    double[] gasPrice = new double[25];
    int priceCount = readPrices(gasPrice);
    double average = averagePrice(gasPrice, priceCount);
    System.out.print("\n\nThe average price per gallon of regular");
    System.out.println(" gas is " + average);
    }


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You will need to learn to place debugging code in your program. For instance, I made these changes:

    Java Code:
       public static double averagePrice(double[] gasPrice, int priceCount) {
    
          double sum = 0;
          System.out.println(java.util.Arrays.toString(gasPrice));  // *** added to check if the array is OK
    
          // Compute the sum
          for (int i = 0; i < priceCount; i++) {
             sum = (gasPrice[priceCount] + sum);
             System.out.println("sum = " + sum);  // **** added to check if sum is getting any data
          }
          // Compute the average
          double average = sum / priceCount;
    And by this way found out that the array was OK, but the sum was not getting anything added to it, suggesting something wrong with the for loop... and indeed there is. Check out the index to the array that you're using in the for loop. It shouldn't be priceCount, but rather should be....

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck!

  4. #4
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag
    Java Code:
     above your pasted code and the tag
    below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Best of luck!


    Thank you very much! I figured it was something like that. You saying where the error was at, really helped me figure it out. The program works fine now.

    Also, I will keep in mind your advice about formating, I'd seen others with code like that, but wasn't sure how to make it look like that on the site.


    Again, thank you very very much

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by LadyMonsterFace View Post
    Thank you very much! I figured it was something like that. You saying where the error was at, really helped me figure it out. The program works fine now.
    Also, I will keep in mind your advice about formating, I'd seen others with code like that, but wasn't sure how to make it look like that on the site.
    Again, thank you very very much
    Your welcome. The most important take-home message I want you get from this thread though is to try out debugging techniques, the simplest of which is to sprinkle your code with println statements.

    Best of luck!

  6. #6
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your welcome. The most important take-home message I want you get from this thread though is to try out debugging techniques, the simplest of which is to sprinkle your code with println statements.

    Best of luck!
    I will for sure! I put that on my "check-list" for checking my java programs

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Require quick program revision please
    By SF163 in forum New To Java
    Replies: 2
    Last Post: 11-07-2009, 12:04 AM
  3. ForLoop statement problem
    By MomenT in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 01:01 PM
  4. Quick Job required in Java
    By taxman in forum Jobs Offered
    Replies: 0
    Last Post: 01-02-2008, 11:46 AM
  5. Help with ForLoop
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •