Results 1 to 7 of 7
  1. #1
    Javawizard is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Array always adding to 0???

    Hi, I'm new to java. Need help.

    My array always adds to 0. I need to be able to add numbers and then do some calculations with it. Calculate average, highest and lowest lap etc. However, it always adds to 0.

    I have this code so far:


    package reversegear;

    public class ReverseGear
    {
    public static void main(String[] args)
    {
    int lap;
    int overall_total = 0;
    int LapTimes[];
    LapTimes = new int[12];
    ConsoleReader console = new ConsoleReader();
    for (lap = 1; lap < 11 ; lap = lap + 1) // The outer loop indexes each lap in the array
    {
    System.out.print("Please enter the lap time for lap " + lap);
    LapTimes[lap] = console.readInt();
    }


    overall_total = overall_total + LapTimes[lap];
    System.out.println("The overall average is : " + (LapTimes[lap])); }

    }

    __________________________________________
    I also have this which was provided to me:


    package reversegear;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class ConsoleReader {
    public ConsoleReader()
    {
    reader = new BufferedReader(new InputStreamReader(System.in));
    }

    private BufferedReader reader;

    public int readInt()
    {
    String inputString = readLine();
    int n = Integer.parseInt(inputString);
    return n;
    }

    public String readLine()
    {
    String inputLine = "";
    try
    {
    inputLine = reader.readLine();

    } catch (IOException e)
    {
    System.out.println(e);
    System.exit(1);
    }
    return inputLine;
    }
    }

    ___________________________________________
    Thanks in advanced

    p.s. am using eclipse....
    Last edited by Javawizard; 11-13-2010 at 07:43 PM. Reason: added lines to read better

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You created an array of 12 elements, but filled just 10 spaces. Incidentally, the array starts with the index 0, not 1. When you exit the for loop, the lap variable is set to 11, which references the last element of the array, which wasn't initialized, and since it's a primitive, you get a default value (0 in this case), instead of a NullPointerException.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Your solution

    What you are printing finally is because of the following line right here

    this line is from ReverseGear.java

    System.out.println("The overall average is : " + (LapTimes[lap]));

    Now let me tell you what this line will do this line it won't print any average it will print the value at the location "LapTimes[12]"

    Reason the value of the variable lap becomes 12 at that point the for loop stops executing and then you are using that variable to check that location in array LapTimes[12],all locations by default in array are set to 0, that is why you always get 0 as the answer.

    If you want the code to actually find the average contact me at
    Total Gyaan: Contact Me

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    The average can be calculated by (total lap time) / (number of laps) i.e. overall_total/laps.
    I'm new to Java but I like to help where ever I can. :)

  5. #5
    Javawizard is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the replies

  6. #6
    Javawizard is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    P.S. I do know how to calculate average... I was just trying to get it to not throw a 0 at me.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    If you want the code to actually find the average contact me at
    Total Gyaan: Contact Me

    @davinkeithlewis: I see a couple of problems with this.

    First, supplying code, whether here or elsewhere, does not address the substantive issue which is the OP's understanding of loops. Providing information so that the OP can write the code he wants (as you did in the body of your post) is far better. Additionally you may (unintentionally) be leading the OP into a situation where he breaks authenticity requirements for assignment work.

    Secondly while the invitation might drive some traffic to your blog, it does so at the expense of what this forum, that you decided to join, is about: discussions open to all to participate in and archived so that they are available for others in the future.

Similar Threads

  1. adding in array String
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 05:28 PM
  2. adding to an array
    By mayhewj7 in forum New To Java
    Replies: 14
    Last Post: 02-19-2009, 06:41 AM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 04:50 AM
  4. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 07:30 PM
  5. Adding graphics to array
    By romina in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 02:45 AM

Tags for this Thread

Posting Permissions

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