Results 1 to 5 of 5
  1. #1
    diwakardelhi is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Question 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.

  2. #2
    diwakardelhi is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Unhappy 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.

  3. #3
    miq.system is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default change

    Java Code:
    int second=1,max=ar[0];
    for(int counter=0; counter<=4;counter++){
    if(max<ar[second]) max=ar[second];
    second++;
    change counter<=4; become counter<4;

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    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

  5. #5
    diwakardelhi is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default thanx

    thanx guys for helping me out and giving tips...
    god bless u all...
    :o

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

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
  •