Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Run Time Error.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Run Time Error.

    This program is used to get the array size and then store some values and get the highest value that the user typed.
    I'm using array and I don't know what's the problem. When in runtime a window pops out and it say ArrayIndexOutofBoundsException. Please help im new in java and im also a student.


    import java.util.*;
    import javax.swing.*;
     
     
     
    public class zy
    {
     
        public static void main(String args[])
        {
     
    		String input,input2;
    		int bck,num,h=0,test=0;
     
    		do{
     
     
    		input=JOptionPane.showInputDialog(null,"Enter Array Size","Array Size",JOptionPane.INFORMATION_MESSAGE);
    		num=Integer.parseInt(input);
     
    		for(int ctr=1;ctr<num+1;ctr++)
    		{
     
    		input2=JOptionPane.showInputDialog(null,"Enter Number " +ctr,"Numbers to test" ,JOptionPane.INFORMATION_MESSAGE);
    		int	num2=Integer.parseInt(input2);
    		int array[]=new int[num2];
     
     
    		if (test==0)
    		{
    			h=array[num2];
    			test=1;
    		}
    		else if (array[num2]>h)
    		{
    			h=array[num2];
    		}
    		}
    		JOptionPane.showMessageDialog(null,"Highest No: " +h,"Highest Number",JOptionPane.INFORMATION_MESSAGE);
    		bck=JOptionPane.showConfirmDialog(null,"Do you want to try again?","CONFIRMATION",JOptionPane.YES_NO_OPTION);
    		h=0;
    		test=0;
    		}while(bck==JOptionPane.YES_OPTION);
        }
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Run Time Error.

    Have you remembered that Java arrays are zero-based? The index of the 1st element in a Java array is 0. The index of the last element in a Java array is whateverYouCalledYourArray[whateverYouCalledYourArray.length - 1] (unless it's a zero-length array, in which case it has no valid indexes)

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    JavaPF (August 2nd, 2011)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,149
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Run Time Error.

    Which line in your program did the error occur on? What was the value of the invalid index?
    The full text of message will say.

  5. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Run Time Error.

    i agree with Sean4u check your index's arrays

  6. #5
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Run Time Error.

    Move your ctrl++ to the end of the for loop, inside the for loops closing bracket. that way on the first run through, ctrl will equal zero.

    You also have an if block that would defeat the purpose of making that change and it should be eliminated.
    Last edited by Spidey1980; August 10th, 2011 at 12:16 PM.

Similar Threads

  1. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  2. [SOLVED] Run time error
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 10:05 AM
  3. [SOLVED] Experiencing a run time error, don't know what the problem is
    By scooty199 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 10:21 AM
  4. Using time
    By ellias2007 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 4th, 2010, 08:48 AM
  5. Struts Application, Complies perfectly but error at run time
    By Prarthana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 18th, 2010, 01:49 AM