Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Ksharp

Thread: ArrayIndexOutOfBoundsException

  1. #1
    finfanman is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException

    I'm trying to get the program to print the difference between two sales figures using an array.
    I can't figure out what the problem is.
    Any help will be greatly appriciated.
    Here is the code


    import java.util.Scanner;

    public class Earnings
    {
    public static void main(String[] args)
    {


    final int salesReps = 2; //Number of sales reps to compare

    double[] sales = new double[salesReps]; //Create array

    Scanner keyboard = new Scanner(System.in); //For keyboard input

    System.out.println("Enter the total sales amount for "
    +salesReps+ " sales representatives to compare. "); //Ask for sales figures

    for (int index = 0; index <salesReps; index++)
    {
    System.out.print("Sales representative " + (index +1) + " sold ");
    sales[index] = keyboard.nextDouble();
    }
    if (sales[0]>sales[2])
    {
    System.out.println("Sales representative 1 sold"
    +(sales[0]-sales[1])+ " more than sales representative 2.");
    }
    else
    {
    System.out.println("Sales representative 2 sold"
    +(sales[1]-sales[0])+ " more than sales representative 1");
    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: ArrayIndexOutOfBoundsException

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException

    if (sales[0]>sales[2]) ----> if (sales[0]>sales[1]) ?
    Fubarable likes this.

  4. #4
    finfanman is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException

    Quote Originally Posted by Ksharp View Post
    if (sales[0]>sales[2]) ----> if (sales[0]>sales[1]) ?
    That's exactly the problem. I can't believe I missed it. Thank you so much!

Similar Threads

  1. what is ArrayIndexOutOfBoundsException ???
    By gauravmanral in forum New To Java
    Replies: 6
    Last Post: 07-16-2011, 07:45 AM
  2. ArrayIndexOutOfBoundsException
    By np2392 in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 10:28 PM
  3. ArrayIndexOutOfBoundsException: 10 ??
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 06:56 PM
  4. ArrayIndexOutOfBoundsException: 0
    By mxsar in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 10:59 PM
  5. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 06:50 PM

Posting Permissions

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