Why I get NZEC error on my code? (java)

Hi, i’m just starting in programming and I don’t know why throws NZEC error, I try to manipulate every error in my code. Thanks for your answers.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.OutOfMemoryError;
public class Main{
public static void main(String[] args) {
    try{
        ArrayList nIngresados=new ArrayList();
        String entrada;
        int stop=0;
        while(stop!=42){
            Scanner entradaTeclado= new Scanner(System.in);
            entrada=entradaTeclado.nextLine();
            try{
                stop=Integer.parseInt(entrada);
                if(stop!=42 && stop<100 && stop>-100)
                    nIngresados.add(stop);
            }
            catch(NumberFormatException nfe){
            }
        }
        int i;
        for(i=0;i<nIngresados.size();i++){
            System.out.println(nIngresados.get(i));
        }
        System.exit(0);
    }
    catch(OutOfMemoryError ome){
        System.exit(0);
    }
System.exit(0);
}
}

hi @bitooh! You are taking string as input and then converting it into int in stop variable.

Everything is correct except the first input which you are taking needs to be added in the array list too.

Correction → In your code before you are checking the condition you have to add the number taken input to array list, then check if its not equal to 42 if true then output the array list otherwise take another input.

here is the corrected version your code with AC. :slight_smile:

oh thanks you so much :smiley: finally I could fix my code and make it work :slight_smile:

1 Like

you are welcome :slight_smile: