Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello to everybody, please somebody can tell me what is wrong with this, i need a program that return the bigger and the smaller number of a array

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

Java
import java.util.*;
public class MNyMY
{
    
    public static void main(String[] args) 
    {   
    	Scanner teclado= new Scanner (System.in);
        int n=0,i=0,y=0,x=0;       
    
        System.out.println("ingrese el valor de el vector");
    	
    	n=teclado.nextInt();
    	
    	int[ ]suma=new int[n];
    	
    	
    	
    	
    	for(i=0;i<=suma.length-1;i=i+1)
    	{
           System.out.println("ingrese el valor de un numero");
          suma[i]=teclado.nextInt();
            
    	}   	
    		if (suma[i]>x);
         {
         	x=suma[i];
         }
         
         
         if (suma[i]<y);>
         {
         	y=suma[i];
         }    		
    	 
    		
    	 System.out.println("este es el menor valor dentro del vector"+y);
         System.out.println("este es el mayor valor dentro del vector"+x);
     
    }
}
Posted
Updated 19-Nov-14 5:49am
v2

1 solution

You should put the mix/max tests inside the for loop:
Java
for(i=0;i<=suma.length-1;i=i+1)
{
   System.out.println("ingrese el valor de un numero");
   suma[i]=teclado.nextInt();
   if (suma[i]>x)
   {
     x=suma[i];
   }
   if (suma[i]<y)
   {
     y=suma[i];
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900