Results 1 to 4 of 4
  1. #1
    BookOfEliX is offline Member
    Join Date
    Feb 2015
    Posts
    20
    Rep Power
    0

    Default Issue with array and math.random.

    When I try to run the program, I am able to input the number of exits, the first exit that they want to take, and the second exit that they want to take. After the last input, i get the following error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at exitsDistance.main(exitsDistance.java:21) Any suggestions?


    Java Code:
    import java.util.*;
    public class exitsDistance{
       public static void main(String[] args){
          Scanner input = new Scanner(System.in);
          System.out.println("please input the number of exits: ");
          int numberOfExitInputs = input.nextInt();
          System.out.println("Please enter the first exit: ");
          int i = input.nextInt();
          System.out.println("Please enter the second exit: ");
          int j = input.nextInt();
          //this array will be created by having the user input the number of exits
          //and using that input for the size of the array.
          int[] a = new int[numberOfExitInputs];
          //randomPositiveValues will be used to generate random numbers
          //that will be eventually stored into the array.
          int randomPositiveValues = (int)(Math.random() * 100);
          //this for loop will be used to store the random numbers
          //generated by math.random into the array.
          //this is where i have the issue. 
          for(int x = 0; x <= a.length; x++){
             a[x] = randomPositiveValues;
          }
          distance(i , j, a);
          
       }
       public static int distance(int i, int j, int[] a){
          int distance = 0;
          distance = i - j;
          return distance; 
       }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Issue with array and math.random.

    Arrays are 0 based so max index is < array.length, not <= array.length.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    BookOfEliX is offline Member
    Join Date
    Feb 2015
    Posts
    20
    Rep Power
    0

    Default Re: Issue with array and math.random.

    omg thanks jim, now if i wanted to display the result from the distance method in the main method, how would i do that?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Issue with array and math.random.

    display the result from the distance method in the main method,
    Save the value returned by the method in a variable and use a println statement to display it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: 08-04-2013, 09:57 AM
  2. Issue with filling array full of random values
    By quinnvanorder in forum New To Java
    Replies: 5
    Last Post: 05-20-2012, 01:04 AM
  3. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-25-2011, 11:10 PM
  4. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 01:57 AM
  5. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 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
  •