0
import java.util.Scanner;
public class RightDominant {


    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int dominant=0,size;
        System.out.print("Enter size number : ");
        size =in.nextInt();
        int right[] = new int[size];
        int sign[] = new int[size];
        right[dominant]=0;        
        for(int i=0;i<size;i++)
        {
            System.out.print("Enter the number ("+(i+1)+") : ");
            sign [i]= in.nextInt();
        }

        for(int i=0;i<size;i++)
        {
            int value=i+1;
                if(value<size)
                {
                    while(sign[i] >= sign[value] )
                    {
                        right[dominant]=sign[i];
                        dominant++;
                        value++;
                    }
                }
        }

        for(int i=0; i<dominant;i++)
        {
            System.out.print("the Right Dominant is : " + right[i]);
        }

    }

}

this Error appear when code run:

Enter size number : 3

Enter the number (1) : 2

Enter the number (2) : 4

Enter the number (3) : 1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at right.dominant.RightDominant.main(RightDominant.java:26) C:\Users\Ayman Saleh\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 6 seconds)

GMc
  • 1,614
  • 1
  • 6
  • 24
  • Usually it is a good idea to add an introductory sentence that summarises what you are doing and the problem. You are likely to get more help because people can get a feel as to whether they can help you or not without having to read the entire post. See [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Specifically the section titled "Introduce the problem before you post any code". – GMc May 08 '19 at 23:40

1 Answers1

0

You have a for loop where i can get above 2 and you are using it as an index into the sign array.

for(int i=0;i<5;i++)  // here you are looping i up to and including 4
{
    int value=i+1;
    while(sign[i] >= sign[value])  // here you are using i (which will fail when it gets to 3
Jason
  • 11,258
  • 3
  • 39
  • 46