Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008, 12:50 AM
Member
 
Join Date: Nov 2007
Posts: 20
Rep Power: 0
Phobos0001 is on a distinguished road
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...

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);
  }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-23-2008, 01:09 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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:
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)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-24-2008, 02:12 AM
Member
 
Join Date: Nov 2007
Posts: 20
Rep Power: 0
Phobos0001 is on a distinguished road
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:
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?

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 02:23 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-24-2008, 02:20 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Help bluegreen7hi New To Java 2 03-28-2008 03:25 AM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 09:59 PM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 03:34 PM
2D array bluekswing New To Java 2 01-15-2008 06:57 PM
Help with Array susan New To Java 1 08-07-2007 05:32 AM


All times are GMT +2. The time now is 03:38 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org