Skip to content
Advertisement

“NZEC” error in Java code during online submission

When I run the following code (in system compiler) it works well, but when I submitted it to the site, all test cases throw nzec runtime error. Unfortunately the site does not share test cases but they can be created using the question.

Can any one please explain why there is a nzec error? I have used java so code is throwing an exception that needs to be caught:

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.text.html.HTMLDocument.Iterator;

public class Readinput1 {

public static int readinput () throws IOException
{
    //InputStreamReader r=new InputStreamReader(System.in);  
    //BufferedReader br=new BufferedReader(r);  
      int n1=0;
      int name=0;
    System.out.println("Enter num");  
    //name=br.read(); 
//  if (name!=null){
    // n1=Integer.parseInt(name.trim());
     //     }
    Scanner in = new Scanner(System.in);
    name= in.nextInt();

    return name;

}


  @SuppressWarnings("unchecked")
  public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
 int num=0;

 ArrayList<Integer> al = new ArrayList<Integer>();
    while (num!=42)
            {
        num=readinput();

                al.add(num);


            }
    //Iterator itr=(Iterator) al.iterator();
    //while(((java.util.Iterator<Integer>) itr).hasNext()){ 
    al.remove(al.size()-1);
    //  String num1=itr.toString();
           //System.out.println(num1);  

     for (Integer integer : al) {
          System.out.println(integer);
        }
          }  


      }

Advertisement

Answer

There are any reason throwing exceptions and produce a so called NZCE.

I don’t give you the solution but pointing on some critical parts.

  • int n1=Integer.parseInt(name); – think about name is not a valid Integer
  • al.remove(al.size()-1); – what’s about the size of al is 0
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement