Results 1 to 4 of 4
  1. #1
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default why is my array out of bounds?

    Hi yet again everyone,
    firstly i'd like to thank everyone for all the help i have recieved in the past, without these forums, i'd have given up on java by now.
    ive been slogging it out trying to get this java array chapter of my textbook done, i have created about 10 fully functional (and very simple) programs using arrays, however this one is giving me grief, i created it the same as my other ones, i get no compile errors, however i enter in the 5th element and boom, i get an error....

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at eightA.main(eightA.java:24)"
    i did lots of googleing and apparently the "eightA.java:24" means the 24th line of code is causing the error.

    well here is the program i am having issues with...

    Java Code:
    //Program's objective is to display the maximum value, 
    //entered in an array
    import javax.swing.*;
    public class eightA
    {
      public static void main(String[] args)
      {
      	final int NUMELS=6;
        int i, max;
        int fmax[] = new int[NUMELS];
        max = fmax[0];
        String s1;
        
        
        for (i = 1; i < fmax.length; i++)
        
        {
          s1 = JOptionPane.showInputDialog("Enter a number: ");
          fmax[i] = Integer.parseInt(s1);
          
          
        }
        
          if (max < fmax[i])
            max = fmax[i];
        
            JOptionPane.showMessageDialog(null, "The maximum value is " + max , "message", 
                                JOptionPane.INFORMATION_MESSAGE);
      }
    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Your error is telling you that the iterator has gone "out of bounds", simply over the array length of limit. Thus, you need to use:
    Java Code:
    for (i = 1; i < fmax.length - 1; i++)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    ah damn, i get ya, thanks for that CaptinMorgan.
    i knew it was going to be something simple! the array works nicely with no problms, however for some reason it only displays "0" as the highest value in the array...

    the program is as follows:
    Java Code:
    //Program's objective is to display the maximum value, 
    //entered in an array
    import javax.swing.*;
    public class eightA
    {
      public static void main(String[] args)
      {
      	final int NUMELS=6;
        int i, max;
        int fmax[] = new int[NUMELS];
        max = fmax[0];
        String s1;
        
        
     
        for (i = 1; i < fmax.length - 1; i++)
        
        {
          s1 = JOptionPane.showInputDialog("Enter a number: ");
          fmax[i] = Integer.parseInt(s1);
          
          
        }
        
          if (max < fmax[i])
            max = fmax[i];
    
            JOptionPane.showMessageDialog(null, "The maximum value is " + max , "message", 
                                JOptionPane.INFORMATION_MESSAGE);
      }
    }

    i dont see why it cannot work, ive done this exact same one before using manualy entered elements in the array and terminal rather then gui...
    there's that program, mabey i did something wrong when i changed it to GUI?

    Java Code:
    public class FindMaxValue
    {
      public static void main(String[] args)
      {
        int i, max;
        int fmax[] = {2, 18, 1, 27, 16};
        max = fmax[0];
        for (i = 1; i < fmax.length; i++)
          if (max < fmax[i])
            max = fmax[i];
        System.out.println("The maximum value is " + max);
      }
    }
    one final question...how would i go about displaying the element's numbered location aswell
    Last edited by Phobos0001; 03-24-2008 at 01:23 AM.

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    No problem Phobos, it happens to us all. ;)

    Cheers,
    -Capt
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 02:25 AM
  2. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 08:59 PM
  3. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM
  4. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 05:57 PM
  5. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •