Results 1 to 5 of 5
Thread: need help in this code
- 11-07-2009, 04:40 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
need help in this code
guys i m new to programming world nd to java...please help me in this simple code. i m trying to find the largest number from the array of numbers from user.i m using eclipse.
the error that i get is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at max.main(max.java:12)
:confused:
import java.util.Scanner;
public class max {
public static void main(String[] args){
int ar[]= new int [5];
Scanner input = new Scanner(System.in);
System.out.println("Please enter 5 numbers.");
for(int count=0; count<5;count++){
ar[count]= input.nextInt();
}
int second=1,max=ar[0];
for(int counter=0; counter<=4;counter++){
if(max<ar[second]) max=ar[second];
second++;
}
System.out.println("The biggest number of the 5 numbers is " + max);
}
}
Last edited by diwakardelhi; 11-07-2009 at 05:17 AM.
- 11-07-2009, 05:40 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
need clarification
guys i was able to solve the problem by changing the array 'ar' length to 7 in 4th line. i.e int ar[]= new int[7];
it worked fine. but it was just hit nd trial method that helped me. this code was not even working with array length 6also. i m still confused why do we have to take an array of 7 length to find maximum of 5 numbers??????
plss help...:confused:Last edited by diwakardelhi; 11-07-2009 at 05:42 AM.
- 11-07-2009, 07:56 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
change
change counter<=4; become counter<4;Java Code:int second=1,max=ar[0]; for(int counter=0; counter<=4;counter++){ if(max<ar[second]) max=ar[second]; second++;
- 11-07-2009, 08:17 AM #4
A recommendation, don't use numbers (e.g. 4) if you are iterating an array. Instead, use array.length or array.length - <some value>. This allows your code to be reused (for different size arrays).
Additionally, Seeing "for(int count=0; count<5;count++){" is confusing because it begs the question "where did the 5 come from?". Whereas, "for(int count=0; count<ar.length;count++){" is very clear, it means you want to do something for each value in the array.CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-08-2009, 08:04 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Why doesn't this code accept my code?
By PeterFeng in forum New To JavaReplies: 5Last Post: 02-03-2009, 01:39 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks