Results 1 to 5 of 5
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default I can't figure out what the error is/ won't print output statements

    I keep getting this error when i comp the code
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Exercise06_19.isSorted(Exercise06_19.java:43)
    at Exercise06_19.main(Exercise06_19.java:26)
    Java Code:
      public static boolean isSorted( int[] list)
      {
         boolean result = false;    
      
        
        for(int i = 0; i < list.length ;i++)
         {    
          if (list[i] < list[i + 1])
           {
             result = true;
           }
         }
        return result;
      }
      public static void main(String[] args)
      {
         Scanner input = new Scanner(System.in);
          System.out.print("Enter list: ");
          int listLength = input.nextInt(); 
          int list1[] = new int [listLength];
     
        for(int i = 0; i < list1.length; i++)
        {
          int numbers = input.nextInt();
          list1[i] = numbers;
        }
    
       if(isSorted(list1) == true)
        {
          System.out.print("The list is sorted");
        }
       else
        {
          System.out.priint("The list is not sorted");
        }
      }
    Last edited by abi; 03-28-2013 at 05:21 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: passing an array from main method to another method

    When passing array to the isSorted() method just pass the name without the angle brackets.

    Java Code:
    ...
    isSorted(list1);
    ...

  3. #3
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: passing an array from main method to another method

    Thank You.

    Are the if statements in my main method wrong??
    i get this error when i run it

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Exercise06_19.isSorted(Exercise06_19.java:20)
    at Exercise06_19.main(Exercise06_19.java:40)

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: passing an array from main method to another method

    ArrayIndexOutOfBoundsException means that you are trying to access element from an array but the index is not valid. Take you code as an example:

    Java Code:
    if (list[i] < list[i + 1])
    You do a loop from 0 to the array length - 1; If the length of array is 6 you will loop from 0 to 5. But what happens where the i = 5? You'll have something like:

    Java Code:
    if (list[5] < list[5 + 1]) // -> This cause an exception
    The statement above will cause an exception because the valid index of array that have six elements it 0 to 5. When you try to access index number 6 it will give you error, saying ArrayIndexOutOfBoundsException.

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: passing an array from main method to another method

    Thank You for the detailed explanation.

Similar Threads

  1. Passing an array through a method
    By vNatural in forum New To Java
    Replies: 5
    Last Post: 02-14-2013, 05:54 AM
  2. Use of passing arguments in main method
    By REICHKONIG in forum New To Java
    Replies: 1
    Last Post: 01-01-2013, 05:50 PM
  3. passing hashmap from a method and call it within main
    By rajuchacha007 in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 10:41 AM
  4. Passing an array to a method.
    By twcast in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 10:13 AM
  5. Replies: 5
    Last Post: 10-04-2008, 12:13 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
  •