Results 1 to 5 of 5
- 03-28-2013, 04:42 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
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.
- 03-28-2013, 04:57 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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); ...
Website: Learn Java by Examples
- 03-28-2013, 05:17 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
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)
- 03-28-2013, 06:31 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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])
Java Code:if (list[5] < list[5 + 1]) // -> This cause an exception
Website: Learn Java by Examples
- 03-28-2013, 09:40 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
Passing an array through a method
By vNatural in forum New To JavaReplies: 5Last Post: 02-14-2013, 05:54 AM -
Use of passing arguments in main method
By REICHKONIG in forum New To JavaReplies: 1Last Post: 01-01-2013, 05:50 PM -
passing hashmap from a method and call it within main
By rajuchacha007 in forum New To JavaReplies: 2Last Post: 04-06-2010, 10:41 AM -
Passing an array to a method.
By twcast in forum New To JavaReplies: 9Last Post: 02-10-2010, 10:13 AM -
[SOLVED] passing array between main and method,vice-versa
By blueyan in forum New To JavaReplies: 5Last Post: 10-04-2008, 12:13 PM
Bookmarks