Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008, 01:50 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 03-23-2008, 02:09 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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++)
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-24-2008, 03:12 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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 03:23 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-24-2008, 03:20 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
No problem Phobos, it happens to us all.

Cheers,
-Capt
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 04:25 AM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 10:59 PM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 04:34 PM
2D array bluekswing New To Java 2 01-15-2008 07:57 PM
Help with Array susan New To Java 1 08-07-2007 06:32 AM


All times are GMT +3. The time now is 04:37 AM.


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